Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Troels http://candyscript.com/

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Calculate all combinations (permutations)

Calculates all combinations, including incomplete results.
   1  
   2  combine([1,2]) == [[1], [2], [1,2]];

   1  
   2  var combine = function(a) {
   3    var fn = function(n, src, got, all) {
   4      if (n == 0) {
   5        if (got.length > 0) {
   6          all[all.length] = got;
   7        }
   8        return;
   9      }
  10      for (var j = 0; j < src.length; j++) {
  11        fn(n - 1, src.slice(j + 1), got.concat([src[j]]), all);
  12      }
  13      return;
  14    }
  15    var all = [];
  16    for (var i=0; i < a.length; i++) {
  17      fn(i, a, [], all);
  18    }
  19    all.push(a);
  20    return all;
  21  }
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS