September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -2,37 +2,38 @@
|
|||
'use strict';
|
||||
|
||||
// permutations :: [a] -> [[a]]
|
||||
const permutations = xs =>
|
||||
xs.length ? concatMap(x => concatMap(ys => [
|
||||
[x].concat(ys)
|
||||
],
|
||||
permutations(delete_(x, xs))), xs) : [
|
||||
[]
|
||||
];
|
||||
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
|
||||
// GENERIC FUNCTIONS ----------------------------------
|
||||
|
||||
// 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);
|
||||
const concatMap = (f, xs) =>
|
||||
xs.reduce((a, x) => a.concat(f(x)), []);
|
||||
|
||||
// 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);
|
||||
// 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 permutations(['Aardvarks', 'eat', 'ants']);
|
||||
return JSON.stringify(
|
||||
permutations(['Aardvarks', 'eat', 'ants'])
|
||||
);
|
||||
})();
|
||||
|
|
|
|||
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