September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
57
Task/Forward-difference/JavaScript/forward-difference.js
Normal file
57
Task/Forward-difference/JavaScript/forward-difference.js
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// forwardDifference :: [Int] -> [Int]
|
||||
const forwardDifference = (n, xs) => {
|
||||
const fd = xs => zipWith((a, b) => a - b, tail(xs), xs);
|
||||
return until(
|
||||
m => m.index < 1,
|
||||
m => ({
|
||||
index: m.index - 1,
|
||||
list: fd(m.list)
|
||||
}), {
|
||||
index: n,
|
||||
list: xs
|
||||
}
|
||||
)
|
||||
.list;
|
||||
};
|
||||
|
||||
|
||||
// GENERIC FUNCTIONS ---------------------------------------
|
||||
|
||||
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
|
||||
const zipWith = (f, xs, ys) => {
|
||||
const ny = ys.length;
|
||||
return (xs.length <= ny ? xs : xs.slice(0, ny))
|
||||
.map((x, i) => f(x, ys[i]));
|
||||
};
|
||||
|
||||
// until :: (a -> Bool) -> (a -> a) -> a -> a
|
||||
const until = (p, f, x) => {
|
||||
const go = x => p(x) ? x : go(f(x));
|
||||
return go(x);
|
||||
};
|
||||
|
||||
// tail :: [a] -> [a]
|
||||
const tail = xs => xs.length ? xs.slice(1) : undefined;
|
||||
|
||||
|
||||
// TEST ----------------------------------------------------
|
||||
|
||||
// range :: Int -> Int -> [Int]
|
||||
const range = (m, n) =>
|
||||
Array.from({
|
||||
length: Math.floor(n - m) + 1
|
||||
}, (_, i) => m + i);
|
||||
|
||||
// show :: a -> String
|
||||
const show = x => JSON.stringify(x);
|
||||
|
||||
// Sample
|
||||
const test = [90, 47, 58, 29, 22, 32, 55, 5, 55, 73];
|
||||
|
||||
return range(1, 9)
|
||||
.map(x => `${x} ${show(forwardDifference(x, test))}`)
|
||||
.join('\n');
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue