September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,19 +1,38 @@
|
|||
(function (lst) {
|
||||
(() => {
|
||||
'use strict';
|
||||
|
||||
const permutations = (xs) => xs.length ? (
|
||||
flatMap((x) => flatMap((xs) => [[x].concat(xs)],
|
||||
permutations(del(x, xs))), xs)
|
||||
) : [[]],
|
||||
// permutations :: [a] -> [[a]]
|
||||
const permutations = xs =>
|
||||
xs.length ? concatMap(x => concatMap(ys => [
|
||||
[x].concat(ys)
|
||||
],
|
||||
permutations(delete_(x, xs))), xs) : [
|
||||
[]
|
||||
];
|
||||
|
||||
flatMap = (f, xs) => [].concat.apply([], xs.map(f)),
|
||||
// GENERIC FUNCTIONS
|
||||
|
||||
del = (x, xs) => xs.length ? x === xs[0] ? (
|
||||
xs.slice(1)
|
||||
) : [xs[0]].concat(del(x, xs.slice(1))
|
||||
// concatMap :: (a -> [b]) -> [a] -> [b]
|
||||
const concatMap = (f, xs) => [].concat.apply([], xs.map(f));
|
||||
//
|
||||
// // delete :: Eq a => a -> [a] -> [a]
|
||||
// const delete_ = (x, xs) =>
|
||||
// deleteBy((a, b) => a === b, x, xs);
|
||||
|
||||
// delete_ :: Eq a => a -> [a] -> [a]
|
||||
const delete_ = (x, xs) =>
|
||||
xs.length > 0 ? (
|
||||
(x === xs[0]) ? (
|
||||
xs.slice(1)
|
||||
) : [xs[0]].concat(delete_(x, xs.slice(1)))
|
||||
) : [];
|
||||
|
||||
// range :: Int -> Int -> [Int]
|
||||
const range = (m, n) =>
|
||||
Array.from({
|
||||
length: Math.floor(n - m) + 1
|
||||
}, (_, i) => m + i);
|
||||
|
||||
return permutations(lst);
|
||||
|
||||
})(["Aardvarks", "eat", "ants"]);
|
||||
// TEST
|
||||
return permutations(['Aardvarks', 'eat', 'ants']);
|
||||
})();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue