Function writing function

  • Thread starter Richard A. DeVenezia
  • Start date
R

Richard A. DeVenezia

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
 
R

Richard Cornford

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

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.
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...

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.
 
R

Richard A. DeVenezia

Richard said:
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?

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.write("<BR>"+n+" permutations")'

perms = new Function (f)

perms()

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

Lasse Reichstein Nielsen

Richard A. DeVenezia said:
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)

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.length;i++) {
acc.push(arr); // push each other element on acc
arr = tmp; // fill hole with elem from above
result = result.concat(perm(arr,acc)); // recurse
arr = acc.pop(); // restore element
}
acc.push(tmp); // push picked element
result = result.concat(perm(arr,acc)); // recurse
arr.push(acc.pop()); // restore arr and acc

return result;
}

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top