Pascal's triangle: Difference between revisions

→‎JS ES5 Functional: Updated primitives
m (→‎JS ES6: ( clearer formatting/commenting of main function ))
(→‎JS ES5 Functional: Updated primitives)
Line 2,383:
 
====Functional====
 
{{Trans|Haskell}}
 
<lang JavaScript>(function (n) {
'use strict';
 
// PASCAL TRIANGLE --------------------------------------------------------
// A Pascal triangle of n rows
 
// pascal :: Int -> [[Int]]
function pascal(n) {
return rangefoldl(1,function n - 1(a) {
.reduce(functionvar xs = (a.slice(-1)[0]; // Previous {row
return append(a, var lstPreviousRow = a.slice[zipWith(-1)[0];
.map( function (xa, ib) {
return ma + ib;
}),
append([0], xs);,
returnappend(xs, a[0])
})]);
}, [[1]]);
[1] // Initial seed row
], enumFromTo(1, n - 1));
};
 
return a
.concat(
[zipWith(
function (a, b) {
return a + b
},
[0].concat(lstPreviousRow),
lstPreviousRow.concat(0)
)]
);
}, [[1]]);
}
 
// GENERIC FUNCTIONS ------------------------------------------------------
 
// range(++) :: Int[a] -> Int[a] -> [Inta]
function rangeappend(mxs, nys) {
return lstPadxs.concat(lineys);
};
 
// enumFromTo :: Int -> Int -> [Int]
function enumFromTo(m, n) {
return Array.applyfrom(null, Array(n - m + 1)){
length: Math.floor(n - m) + 1
}, function (a_, bi) {
return am + bi;
}),;
};
 
// 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) {
return b < a false,? b : a;
};
 
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
function zipWith(f, xs, ys) {
return xsArray.length === ys.length ? from({
length: min(xs.map(function (xlength, iys.length) {
}, function return f(x_, ys[i]); {
}return f(xs[i], ys[i]);
}) : undefined;
};
 
// range :: Int -> Int -> [Int]
function range(m, n) {
return Array.apply(null, Array(n - m + 1))
.map(function (x, i) {
return m + i;
});
}
 
// TEST and FORMAT --------------------------------------------------------
// TEST
var lstTriangle = pascal(n);
 
 
// FORMAT OUTPUT AS WIKI TABLE
 
// [[a]] -> bool -> s -> s
function wikiTable(lstRows, blnHeaderRow, strStyle) {
return '{| class="wikitable" ' + (strStyle ? 'style="' + strStyle +
strStyle'"' ?: 'style="') + strStylelstRows.map(function +(lstRow, '"'iRow) : ''{
) + lstRows.map(function (lstRow, var strDelim = blnHeaderRow && !iRow) {? '!' : '|';
varreturn strDelim'\n|-\n' =+ ((blnHeaderRowstrDelim &&+ !iRow)' ? '!' :+ '|'lstRow.map(function (v); {
 
return '\n|-\n' + strDelim + ' ' + lstRow.map(function (
v) {
return typeof v === 'undefined' ? ' ' : v;
})
Line 2,454 ⟶ 2,468:
 
var lstLastLine = lstTriangle.slice(-1)[0],
lngBase = (lstLastLine.length * 2) - 1,
nWidth = lstLastLine.reduce(function (a, x) {
var d = x.toString()
Line 2,461 ⟶ 2,475:
}, 1) * lngBase;
 
return [wikiTable(lstTriangle.map(function (lst) {
return lst.join(';;')]
wikiTable(
lstTriangle .mapsplit(function (lst';') {;
return lst.join(';;'})
.map(function (line, i) .split(';');{
}var lstPad = Array((lngBase - line.length) / 2);
return lstPad.map(function concat(line, i) {
var .concat(lstPad = Array((lngBase - line.length) / 2);
}), false, 'text-align:center;width:' + nWidth + 'em;height:' + nWidth +
return lstPad.concat(line)
'em;table-layout:fixed;'), JSON.stringify(lstTriangle)].join('\n\n');
.concat(lstPad);
}),
false,
'text-align:center;width:' + nWidth + 'em;height:' + nWidth +
'em;table-layout:fixed;'
),
 
JSON.stringify(lstTriangle)
].join('\n\n');
})(7);</lang>
{{Out}}
 
Output:
 
{| class="wikitable" style="text-align:center;width:26em;height:26em;table-layout:fixed;"
|-
Line 2,501 ⟶ 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>
 
 
===ES6===
9,655

edits