2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -0,0 +1,61 @@
(function () {
'use strict';
// [n] -> [[[n]]]
function partitions(a1, a2, a3) {
var n = a1 + a2 + a3;
return combos(range(1, n), n, [a1, a2, a3]);
}
function combos(s, n, xxs) {
if (!xxs.length) return [[]];
var x = xxs[0],
xs = xxs.slice(1);
return mb( choose(s, n, x), function (l_rest) {
return mb( combos(l_rest[1], (n - x), xs), function (r) {
// monadic return/injection requires 1 additional
// layer of list nesting:
return [ [l_rest[0]].concat(r) ];
})});
}
function choose(aa, n, m) {
if (!m) return [[[], aa]];
var a = aa[0],
as = aa.slice(1);
return n === m ? (
[[aa, []]]
) : (
choose(as, n - 1, m - 1).map(function (xy) {
return [[a].concat(xy[0]), xy[1]];
}).concat(choose(as, n - 1, m).map(function (xy) {
return [xy[0], [a].concat(xy[1])];
}))
);
}
// GENERIC
// Monadic bind (chain) for lists
function mb(xs, f) {
return [].concat.apply([], xs.map(f));
}
// [m..n]
function range(m, n) {
return Array.apply(null, Array(n - m + 1)).map(function (x, i) {
return m + i;
});
}
// EXAMPLE
return partitions(2, 0, 2);
})();

View file

@ -0,0 +1,6 @@
[[[1, 2], [], [3, 4]],
[[1, 3], [], [2, 4]],
[[1, 4], [], [2, 3]],
[[2, 3], [], [1, 4]],
[[2, 4], [], [1, 3]],
[[3, 4], [], [1, 2]]]