2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
47
Task/Combinations/JavaScript/combinations-6.js
Normal file
47
Task/Combinations/JavaScript/combinations-6.js
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
(function (n) {
|
||||
'use strict';
|
||||
|
||||
|
||||
// n -> [a] -> [[a]]
|
||||
let comb = (n, xs) => {
|
||||
if (n < 1) return [[]];
|
||||
if (xs.length === 0) return [];
|
||||
|
||||
let h = xs[0],
|
||||
tail = xs.slice(1);
|
||||
|
||||
return comb(n - 1, tail)
|
||||
.map((t) => [h].concat(t))
|
||||
.concat(comb(n, tail));
|
||||
},
|
||||
|
||||
|
||||
|
||||
// Derive a memoized version of a function
|
||||
// Function -> Function
|
||||
memoized = (f) => {
|
||||
let m = {};
|
||||
|
||||
return function (x) {
|
||||
let args = [].slice.call(arguments),
|
||||
strKey = args.join('-'),
|
||||
v = m[strKey];
|
||||
|
||||
return (
|
||||
(v === undefined) &&
|
||||
(m[strKey] = v = f.apply(null, args)),
|
||||
v
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
range = (m, n) =>
|
||||
Array.from({
|
||||
length: (n - m) + 1
|
||||
}, (_, i) => m + i);
|
||||
|
||||
|
||||
return memoized(comb)(n, range(0, 4))
|
||||
|
||||
|
||||
})(3);
|
||||
Loading…
Add table
Add a link
Reference in a new issue