Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
35
Task/Permutations/JavaScript/permutations-4.js
Normal file
35
Task/Permutations/JavaScript/permutations-4.js
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
(function () {
|
||||
'use strict';
|
||||
|
||||
// permutations :: [a] -> [[a]]
|
||||
var permutations = function (xs) {
|
||||
return xs.length ? concatMap(function (x) {
|
||||
return concatMap(function (ys) {
|
||||
return [[x].concat(ys)];
|
||||
}, permutations(delete_(x, xs)));
|
||||
}, xs) : [[]];
|
||||
};
|
||||
|
||||
// GENERIC FUNCTIONS
|
||||
|
||||
// concatMap :: (a -> [b]) -> [a] -> [b]
|
||||
var concatMap = function (f, xs) {
|
||||
return [].concat.apply([], xs.map(f));
|
||||
};
|
||||
|
||||
// delete :: Eq a => a -> [a] -> [a]
|
||||
var delete_ = function (x, xs) {
|
||||
return deleteBy(function (a, b) {
|
||||
return a === b;
|
||||
}, x, xs);
|
||||
};
|
||||
|
||||
// deleteBy :: (a -> a -> Bool) -> a -> [a] -> [a]
|
||||
var deleteBy = function (f, x, xs) {
|
||||
return xs.length > 0 ? f(x, xs[0]) ? xs.slice(1) :
|
||||
[xs[0]].concat(deleteBy(f, x, xs.slice(1))) : [];
|
||||
};
|
||||
|
||||
// TEST
|
||||
return permutations(['Aardvarks', 'eat', 'ants']);
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue