Water collected between towers: Difference between revisions
Content added Content deleted
Alpha bravo (talk | contribs) |
m (→JS ES6) |
||
Line 1,762: | Line 1,762: | ||
{{Trans|Haskell}} |
{{Trans|Haskell}} |
||
<lang JavaScript>(() => { |
<lang JavaScript>(() => { |
||
"use strict"; |
|||
// |
// --------- WATER COLLECTED BETWEEN TOWERS ---------- |
||
// waterCollected :: [Int] -> Int |
// waterCollected :: [Int] -> Int |
||
Line 1,779: | Line 1,779: | ||
// |
// ---------------------- TEST ----------------------- |
||
const main = () => [ |
const main = () => [ |
||
[1, 5, 3, 7, 2], |
[1, 5, 3, 7, 2], |
||
Line 1,791: | Line 1,791: | ||
// |
// --------------------- GENERIC --------------------- |
||
// Tuple (,) :: a -> b -> (a, b) |
// Tuple (,) :: a -> b -> (a, b) |
||
const Tuple = a => |
const Tuple = a => |
||
b => ({ |
b => ({ |
||
type: |
type: "Tuple", |
||
"0": a, |
|||
"1": b, |
|||
length: 2 |
length: 2 |
||
}); |
}); |
||
Line 1,808: | Line 1,808: | ||
// the predicate p. |
// the predicate p. |
||
xs => [...xs].filter(p); |
xs => [...xs].filter(p); |
||
// gt :: Ord a => a -> a -> Bool |
|||
const gt = x => y => |
|||
'Tuple' === x.type ? ( |
|||
⚫ | |||
) : (x > y); |
|||
// length :: [a] -> Int |
|||
const length = xs => |
|||
// Returns Infinity over objects without finite |
|||
// length. This enables zip and zipWith to choose |
|||
// the shorter argument when one is non-finite, |
|||
// like cycle, repeat etc |
|||
'GeneratorFunction' !== xs.constructor |
|||
.constructor.name ? ( |
|||
xs.length |
|||
) : Infinity; |
|||
Line 1,850: | Line 1,831: | ||
xs.reduce((a, x) => { |
xs.reduce((a, x) => { |
||
const v = f(a[0])(x); |
const v = f(a[0])(x); |
||
return Tuple(v)(a[1].concat(v)); |
return Tuple(v)(a[1].concat(v)); |
||
}, Tuple(startValue)([startValue]))[1]; |
}, Tuple(startValue)([startValue]))[1]; |
||
Line 1,856: | Line 1,838: | ||
// scanl1 :: (a -> a -> a) -> [a] -> [a] |
// scanl1 :: (a -> a -> a) -> [a] -> [a] |
||
const scanl1 = f => |
const scanl1 = f => |
||
// scanl1 is a variant of scanl that |
// scanl1 is a variant of scanl that |
||
// starting value argument. |
// has no starting value argument. |
||
xs => xs.length > 0 ? ( |
xs => xs.length > 0 ? ( |
||
scanl(f)( |
scanl(f)( |
||
Line 1,865: | Line 1,847: | ||
// scanr :: ( |
// scanr :: (a -> b -> b) -> b -> [a] -> [b] |
||
const scanr = f => |
const scanr = f => |
||
startValue => xs => xs.reduceRight( |
startValue => xs => xs.reduceRight( |
||
(a, x) => { |
|||
const v = f(x)(a[0]); |
|||
⚫ | |||
return Tuple(v)([v].concat(a[1])); |
|||
⚫ | |||
⚫ | |||
// scanr1 :: (a -> a -> a) -> [a] -> [a] |
// scanr1 :: (a -> a -> a) -> [a] -> [a] |
||
const scanr1 = f => |
const scanr1 = f => |
||
// scanr1 is a variant of scanr that has no |
// scanr1 is a variant of scanr that has no |
||
// seed-value argument, and assumes that |
// seed-value argument, and assumes that |
||
// xs is not empty. |
// xs is not empty. |
||
Line 1,894: | Line 1,879: | ||
// The numeric sum of all values in xs. |
// The numeric sum of all values in xs. |
||
xs.reduce((a, x) => a + x, 0); |
xs.reduce((a, x) => a + x, 0); |
||
// take :: Int -> [a] -> [a] |
|||
const take = n => |
|||
// The first n elements of a list, |
|||
// string of characters, or stream. |
|||
xs => xs.slice(0, n); |
|||
Line 1,908: | Line 1,886: | ||
// custom function, rather than with the |
// custom function, rather than with the |
||
// default tuple constructor. |
// default tuple constructor. |
||
xs => ys => ( |
xs => ys => xs.map( |
||
(x, i) => f(x)(ys[i]) |
|||
).slice( |
|||
0, Math.min(xs.length, ys.length) |
|||
); |
|||
})(xs, ys); |
|||
// MAIN --- |
// MAIN --- |