Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
39
Task/Permutations/JavaScript/permutations-6.js
Normal file
39
Task/Permutations/JavaScript/permutations-6.js
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// permutations :: [a] -> [[a]]
|
||||
const permutations = xs => {
|
||||
const go = xs => xs.length ? (
|
||||
concatMap(
|
||||
x => concatMap(
|
||||
ys => [[x].concat(ys)],
|
||||
go(delete_(x, xs))), xs
|
||||
)
|
||||
) : [[]];
|
||||
return go(xs);
|
||||
};
|
||||
|
||||
// GENERIC FUNCTIONS ----------------------------------
|
||||
|
||||
// concatMap :: (a -> [b]) -> [a] -> [b]
|
||||
const concatMap = (f, xs) =>
|
||||
xs.reduce((a, x) => a.concat(f(x)), []);
|
||||
|
||||
|
||||
// delete :: Eq a => a -> [a] -> [a]
|
||||
const delete_ = (x, xs) => {
|
||||
const go = xs => {
|
||||
return 0 < xs.length ? (
|
||||
(x === xs[0]) ? (
|
||||
xs.slice(1)
|
||||
) : [xs[0]].concat(go(xs.slice(1)))
|
||||
) : [];
|
||||
}
|
||||
return go(xs);
|
||||
};
|
||||
|
||||
// TEST
|
||||
return JSON.stringify(
|
||||
permutations(['Aardvarks', 'eat', 'ants'])
|
||||
);
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue