Pascal's triangle: Difference between revisions
→JS ES6: Updated primitives
(→JS ES6: Updated primitives) |
|||
Line 2,504:
===ES6===
<lang JavaScript>(() => {
'use strict';
// PASCAL'S TRIANGLE ------------------------------------------------------
// pascal :: Int -> [[Int]]▼
range(1, n - 1)▼
.reduce(a => {▼
// pascal :: Int ->
const lstPreviousRow =
return append(a,
append(lstPreviousRow, [
]);
[1]
// GENERIC FUNCTIONS ------------------------------------------------------
//
return Array.from({▼
}, (_, i) => m + (i * d));▼
▲ },
const
length:
}, (_, i) =>
// foldl :: (b -> a -> b) -> b -> [a] -> b
▲ // TEST
const foldl = (f, a, xs) => xs.reduce(f, a);
return pascal(7)▼
let strIndent = a.indent;▼
// foldr (a -> b -> b) -> b -> [a] -> b
const foldr = (f, a, xs) => xs.reduceRight(f, a);
// map :: (a -> b) -> [a] -> [b]
const map = (f, xs) => xs.map(f);
// min :: Ord a => a -> a -> a
const min = (a, b) => b < a ? b : a;
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
const zipWith = (f, xs, ys) =>
length: min(xs.length, ys.length)
// TEST -------------------------------------------------------------------
return {
rows: strIndent +
.join('') + '\n' + a.rows,
indent: strIndent + ' '
Line 2,554 ⟶ 2,563:
rows: '',
indent: ''
}, pascal(7))
.rows;
})();</lang>
{{Out}}
<pre> 1
|