Permutations: Difference between revisions
→JS ES6: Updated and pruned primitives, confined recursion to local scope, tidied slightly.
(→Functional Python: Shed one pair of redundant brackets) |
(→JS ES6: Updated and pruned primitives, confined recursion to local scope, tidied slightly.) |
||
Line 3,235:
===ES6===
Recursively, in terms of concatMap and delete:
<lang JavaScript>(() => {
'use strict';
// permutations :: [a] -> [[a]]
const permutations = xs => {
) : [[]];▼
return go(xs);
// GENERIC FUNCTIONS ----------------------------------
// concatMap :: (a -> [b]) -> [a] -> [b]
const concatMap = (f, xs) =>
▲ //
▲ // deleteBy((a, b) => a === b, x, xs);
(x === xs[0]) ? (▼
xs.slice(1)▼
) : [xs[0]].concat(delete_(x, xs.slice(1)))▼
▲ ) : [];
//
const
▲ xs.slice(1)
}
return go(xs);
};
// TEST
return
permutations(['Aardvarks', 'eat', 'ants'])
);
})();</lang>
{{Out}}
<lang JavaScript>[["Aardvarks", "eat", "ants"], ["Aardvarks", "ants", "eat"],
|