Pascal's triangle: Difference between revisions
Content added Content deleted
m (→JS ES6: ( clearer formatting/commenting of main function )) |
(→JS ES5 Functional: Updated primitives) |
||
Line 2,383: | Line 2,383: | ||
====Functional==== |
====Functional==== |
||
{{Trans|Haskell}} |
{{Trans|Haskell}} |
||
<lang JavaScript>(function (n) { |
<lang JavaScript>(function (n) { |
||
'use strict'; |
'use strict'; |
||
// PASCAL TRIANGLE -------------------------------------------------------- |
|||
// A Pascal triangle of n rows |
|||
// pascal :: Int -> [[Int]] |
// pascal :: Int -> [[Int]] |
||
function pascal(n) { |
function pascal(n) { |
||
return |
return foldl(function (a) { |
||
var xs = a.slice(-1)[0]; // Previous row |
|||
return append(a, [zipWith( |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
[1] // Initial seed row |
|||
], enumFromTo(1, n - 1)); |
|||
⚫ | |||
⚫ | |||
.concat( |
|||
[zipWith( |
|||
function (a, b) { |
|||
return a + b |
|||
}, |
|||
[0].concat(lstPreviousRow), |
|||
lstPreviousRow.concat(0) |
|||
)] |
|||
⚫ | |||
⚫ | |||
⚫ | |||
// GENERIC FUNCTIONS ------------------------------------------------------ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
// enumFromTo :: Int -> Int -> [Int] |
|||
function enumFromTo(m, n) { |
|||
⚫ | |||
length: Math.floor(n - m) + 1 |
|||
⚫ | |||
⚫ | |||
⚫ | |||
}; |
|||
// foldl :: (b -> a -> b) -> b -> [a] -> b |
|||
function foldl(f, a, xs) { |
|||
return xs.reduce(f, a); |
|||
}; |
|||
// foldr (a -> b -> b) -> b -> [a] -> b |
|||
// GENERIC FUNCTIONS |
|||
function foldr(f, a, xs) { |
|||
return xs.reduceRight(f, a); |
|||
}; |
|||
// map :: (a -> b) -> [a] -> [b] |
|||
function map(f, xs) { |
|||
return xs.map(f); |
|||
}; |
|||
// min :: Ord a => a -> a -> a |
|||
function min(a, b) { |
|||
⚫ | |||
}; |
|||
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] |
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] |
||
function zipWith(f, xs, ys) { |
function zipWith(f, xs, ys) { |
||
return |
return Array.from({ |
||
xs. |
length: min(xs.length, ys.length) |
||
}, function (_, i) { |
|||
return f(xs[i], ys[i]); |
|||
) |
}); |
||
} |
}; |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
// TEST and FORMAT -------------------------------------------------------- |
|||
// TEST |
|||
var lstTriangle = pascal(n); |
var lstTriangle = pascal(n); |
||
// FORMAT OUTPUT AS WIKI TABLE |
|||
// [[a]] -> bool -> s -> s |
// [[a]] -> bool -> s -> s |
||
function wikiTable(lstRows, blnHeaderRow, strStyle) { |
function wikiTable(lstRows, blnHeaderRow, strStyle) { |
||
return '{| class="wikitable" ' + ( |
return '{| class="wikitable" ' + (strStyle ? 'style="' + strStyle + |
||
'"' : '') + lstRows.map(function (lstRow, iRow) { |
|||
var strDelim = blnHeaderRow && !iRow ? '!' : '|'; |
|||
return '\n|-\n' + strDelim + ' ' + lstRow.map(function (v) { |
|||
return '\n|-\n' + strDelim + ' ' + lstRow.map(function ( |
|||
v) { |
|||
return typeof v === 'undefined' ? ' ' : v; |
return typeof v === 'undefined' ? ' ' : v; |
||
}) |
}) |
||
Line 2,454: | Line 2,468: | ||
var lstLastLine = lstTriangle.slice(-1)[0], |
var lstLastLine = lstTriangle.slice(-1)[0], |
||
lngBase = |
lngBase = lstLastLine.length * 2 - 1, |
||
nWidth = lstLastLine.reduce(function (a, x) { |
nWidth = lstLastLine.reduce(function (a, x) { |
||
var d = x.toString() |
var d = x.toString() |
||
Line 2,461: | Line 2,475: | ||
}, 1) * lngBase; |
}, 1) * lngBase; |
||
return [ |
return [wikiTable(lstTriangle.map(function (lst) { |
||
⚫ | |||
wikiTable( |
|||
.split(';'); |
|||
}) |
|||
.map(function (line, i) { |
|||
var lstPad = Array((lngBase - line.length) / 2); |
|||
. |
return lstPad.concat(line) |
||
.concat(lstPad); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
.concat(lstPad); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
'em;table-layout:fixed;' |
|||
⚫ | |||
⚫ | |||
].join('\n\n'); |
|||
})(7);</lang> |
})(7);</lang> |
||
{{Out}} |
|||
Output: |
|||
{| class="wikitable" style="text-align:center;width:26em;height:26em;table-layout:fixed;" |
{| class="wikitable" style="text-align:center;width:26em;height:26em;table-layout:fixed;" |
||
|- |
|- |
||
Line 2,501: | Line 2,505: | ||
<lang JavaScript>[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1],[1,5,10,10,5,1],[1,6,15,20,15,6,1]]</lang> |
<lang JavaScript>[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1],[1,5,10,10,5,1],[1,6,15,20,15,6,1]]</lang> |
||
===ES6=== |
===ES6=== |