Anton Harald
Anton Harald

Reputation: 5944

iterate through all permutations of two variables

I need to call the following function cross 4 times, for all permutations of the x and y variable with the values 1 and -1;

my approach:

var p = [-1, 1];

p.forEach(function(x) {
  p.forEach(function(y) {
    cross(x, y);
  });
});

Is there a shorter way of doing this?

Upvotes: 5

Views: 370

Answers (4)

c-smile
c-smile

Reputation: 27460

This will be faster:

for (var n = 0; n < 4; ++n) 
     cross(n & 2 ? 1:-1, n & 1 ? -1:1);

but is it shorter?

Upvotes: 1

ssube
ssube

Reputation: 48277

If you want to be extra functional, you can use map and then reduce the arrays into one. I don't think it would necessarily be any more efficient than what you have now, nor is it much simpler (it is more functional, just slightly).

var d = [-1, 1];

var r = d.reduce(function(p, x) {
  return p.concat(d.map(function(y) {
    return cross(x, y);
  }));
}, []);

document.getElementById('r').textContent = JSON.stringify(r);
<pre id=r></pre>

I really don't think there is an algorithm with better than n^2 efficiency to produce n^2 combinations.

Upvotes: 3

bhspencer
bhspencer

Reputation: 13560

Since you know the length of your array p you don't need a loop. The fastest solution would be:

var p = [-1, 1];
cross(p[0], p[0]);
cross(p[0], p[1]);
cross(p[1], p[0]);
cross(p[1], p[1]);

Upvotes: 2

bhspencer
bhspencer

Reputation: 13560

There is some overhead in calling a function. e.g. putting the return pointer on the stack. It would probably be slightly faster to use two for loops rather than callbacks to forEach.

var p = [-1, 1];
for (var x = 0; x < p.length; x++) {
    for (var y = 0; y < p.length; y++) {
        cross(p[x], p[y]);
    } 
}

Upvotes: 1

Related Questions