Water collected between towers: Difference between revisions
m
→JS ES6
Alpha bravo (talk | contribs) |
m (→JS ES6) |
||
Line 1,762:
{{Trans|Haskell}}
<lang JavaScript>(() => {
//
// waterCollected :: [Int] -> Int
Line 1,779:
//
const main = () => [
[1, 5, 3, 7, 2],
Line 1,791:
//
// Tuple (,) :: a -> b -> (a, b)
const Tuple = a =>
b => ({
type:
length: 2
});
Line 1,808:
// the predicate p.
xs => [...xs].filter(p);
x[0] > y[0]▼
Line 1,850 ⟶ 1,831:
xs.reduce((a, x) => {
const v = f(a[0])(x);
return Tuple(v)(a[1].concat(v));
}, Tuple(startValue)([startValue]))[1];
Line 1,856 ⟶ 1,838:
// scanl1 :: (a -> a -> a) -> [a] -> [a]
const scanl1 = f =>
// scanl1 is a variant of scanl that
// has no starting value argument.
xs => xs.length > 0 ? (
scanl(f)(
Line 1,865 ⟶ 1,847:
// scanr :: (
const scanr = f =>
startValue => xs => xs.reduceRight(
}, Tuple(startValue)([startValue]))[1];▼
return Tuple(v)([v].concat(a[1]));
// scanr1 :: (a -> a -> a) -> [a] -> [a]
const scanr1 = f =>
// scanr1 is a variant of scanr that has no
// seed-value argument, and assumes that
// xs is not empty.
Line 1,894 ⟶ 1,879:
// The numeric sum of all values in xs.
xs.reduce((a, x) => a + x, 0);
Line 1,908 ⟶ 1,886:
// custom function, rather than with the
// default tuple constructor.
xs => ys => xs.map(
0,
// MAIN ---
|