Function writing function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard A. DeVenezia

    Function writing function

    Can a function write another function that has a specific number of
    nested loops and then run it?

    i.e.
    function maker (N) {
    // does stuff that creates function doer()
    // invoke doer
    doer()
    }

    For a specific value of N, doer might have this construction:

    function doer() {
    for (i0=0;i0<x;i0++ {
    for (i1=0;i1<x;i1++ {
    ...
    for (i#N#=0;i#N#<x; i#N#++ {

    // stuff

    }...}} // N closing braces
    }

    i#N# simply means replace #N# with what ever value of N is.

    So for N=4, I would want 4 nested loops, for N=5 I would want 5 nested
    loops, etc...


    Thanks,
    Richard
  • Richard Cornford

    #2
    Re: Function writing function

    Richard A. DeVenezia wrote:[color=blue]
    > Can a function write another function that has a specific
    > number of nested loops and then run it?[/color]

    The - Function - constructor can be invoked with a string holding the
    text of source code as its final argument and will create a function
    object using that code as the function body (except possibly on systems
    implementing the ECMAScript compact profile (ECMA 327) and taking
    advantage of the option not to facilitate the use of the Function
    constructor).

    In any real problem situation the use of the - Function - constructor is
    unlikely to be necessary or optimal. Javascript is so dynamic in its
    nature that creating functions from strings would usually be avoidable
    and the overhead in string manipulation would be difficult to justify
    unless the resulting function was destined for repeated use.
    [color=blue]
    > i.e.
    > function maker (N) {
    > // does stuff that creates function doer()
    > // invoke doer
    > doer()
    > }
    >
    > For a specific value of N, doer might have this construction:
    >
    > function doer() {
    > for (i0=0;i0<x;i0++ {
    > for (i1=0;i1<x;i1++ {
    > ...
    > for (i#N#=0;i#N#<x; i#N#++ {
    >
    > // stuff
    >
    > }...}} // N closing braces
    > }
    >
    > i#N# simply means replace #N# with what ever value of N is.
    >
    > So for N=4, I would want 4 nested loops, for N=5 I would want
    > 5 nested loops, etc...[/color]

    Identifying a better alternative hangs of the unspecified "stuff" that
    if to happen within the innermost loop. How is it going to take
    advantage of loop counter i#N#, and if it doesn't why are the nested
    loops needed?

    Richard.


    Comment

    • Richard A. DeVenezia

      #3
      Re: Function writing function

      Richard Cornford wrote:
      [color=blue]
      > Identifying a better alternative hangs of the unspecified "stuff" that
      > if to happen within the innermost loop. How is it going to take
      > advantage of loop counter i#N#, and if it doesn't why are the nested
      > loops needed?[/color]

      This function writes a function that enumerates permutations of n things by
      iterations.
      Yes, the dense interaction of abstract and specific is a difficult brew to
      understand and maintain.
      Yes, there are recursive techniques that can do the same thing (I haven't
      done any benchmarking to see if lots of loops with lots of ifs is faster
      than lots of loops of recursive function calls)

      <html>
      <head>
      <title>Perms</title>
      <script type="text/javascript">
      function Perms (n) {

      var f = '// Permutations of ' + n + ' things';
      f += '\nvar n=0'

      for (var i=0;i<n;i++) {
      f += '\nfor (i'+i+'=0;i'+i+ '<'+n+';i'+i+'+ +) {'

      for (var j=0;j<i;j++) {
      f += '\nif (i'+i+'==i'+j+' ) continue'
      }
      }

      f += '\nn++'
      f += '\n/**/ document.write ("<BR>"+n+": "'

      for (var i=0;i<n;i++) {
      if (i) f += '+","'
      f += '+i'+i
      }

      f += ') /**/'

      f += '\n'

      for (var i=0;i<n;i++) {
      f += '}'
      }

      f += '\ndocument.wri te("<BR>"+n+" permutations")'

      perms = new Function (f)

      perms()

      document.write ('<PRE>'+perms. toString().repl ace(/</g,"&lt;")+'</PRE>')
      }
      </script>
      </head>
      <body>
      <script type="text/javascript">
      Perms(4)
      </script>
      </body>
      </html>

      --
      Richard A. DeVenezia


      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: Function writing function

        "Richard A. DeVenezia" <radevenz@ix.ne tcom.com> writes:
        [color=blue]
        > This function writes a function that enumerates permutations of n things by
        > iterations.
        > Yes, the dense interaction of abstract and specific is a difficult brew to
        > understand and maintain.[/color]
        [color=blue]
        > Yes, there are recursive techniques that can do the same thing (I haven't
        > done any benchmarking to see if lots of loops with lots of ifs is faster
        > than lots of loops of recursive function calls)[/color]

        My guess is that the recursive version will be simpler and therefore
        faster.

        The way you generate ifs, the inner loop (which will run n times) will
        start with n-1 if statements that will continue the loop in all but
        one case. There will be approx. (1/2)*n^2 if statements, which will all
        have been tested for each permutation you generate, as well as all the
        tests that fail and continue a loop. I believe the time complexity
        is quite bad for this approach.

        Here is a recursive function for comparison :)
        ---
        function perm(arr,acc) { // arr(ay): elements yet to permute
        // acc(umulator): elements placed so far
        if (!acc) {acc=[];}
        if (arr.length == 0) {
        return [acc.slice(0)]; // return copy of accumulator
        }
        var result = [];
        var tmp = arr.pop(); // pick element to fill hole with
        for(var i=0;i<arr.lengt h;i++) {
        acc.push(arr[i]); // push each other element on acc
        arr[i] = tmp; // fill hole with elem from above
        result = result.concat(p erm(arr,acc)); // recurse
        arr[i] = acc.pop(); // restore element
        }
        acc.push(tmp); // push picked element
        result = result.concat(p erm(arr,acc)); // recurse
        arr.push(acc.po p()); // restore arr and acc

        return result;
        }

        alert(perm([0,1,2,3]).join("\n"));
        ---

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        Working...