Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
42
Task/Permutations/JavaScript/permutations-8.js
Normal file
42
Task/Permutations/JavaScript/permutations-8.js
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// permutations :: [a] -> [[a]]
|
||||
const permutations = xs =>
|
||||
xs.reduceRight(
|
||||
(a, x) => concatMap(
|
||||
xs => enumFromTo(0, xs.length)
|
||||
.map(n => xs.slice(0, n)
|
||||
.concat(x)
|
||||
.concat(xs.slice(n))
|
||||
),
|
||||
a
|
||||
),
|
||||
[[]]
|
||||
);
|
||||
|
||||
// GENERIC FUNCTIONS ----------------------------------
|
||||
|
||||
// concatMap :: (a -> [b]) -> [a] -> [b]
|
||||
const concatMap = (f, xs) =>
|
||||
xs.reduce((a, x) => a.concat(f(x)), []);
|
||||
|
||||
// ft :: Int -> Int -> [Int]
|
||||
const enumFromTo = (m, n) =>
|
||||
Array.from({
|
||||
length: 1 + n - m
|
||||
}, (_, i) => m + i);
|
||||
|
||||
// showLog :: a -> IO ()
|
||||
const showLog = (...args) =>
|
||||
console.log(
|
||||
args
|
||||
.map(JSON.stringify)
|
||||
.join(' -> ')
|
||||
);
|
||||
|
||||
// TEST -----------------------------------------------
|
||||
showLog(
|
||||
permutations([1, 2, 3])
|
||||
);
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue