Pascal's triangle: Difference between revisions

→‎Functional (ES5): Simpler Pascal function (translating one of the Haskell versions)
(→‎Functional (ES5): Simpler Pascal function (translating one of the Haskell versions))
Line 2,167:
 
===Functional (ES5)===
 
 
{{Trans|Haskell}}
 
 
<lang JavaScript>(function (n) {
'use strict';
 
// A Pascal triangle of n rows
// n --> [[n]]
function pascalTriangle(n) {
 
// Sumspascal of:: eachInt consecutive-> pair of numbers[[Int]]
//function [pascal(n]) --> [n]{
return range(1, n - 1)
function pairSums(lst) {
return lst .reduce(function (acc, n, i, la) {
var iPrev = i ? i - 1 :var lstPreviousRow = a.slice(-1)[0];
 
return i ? acc.concat(l[iPrev] + l[i]) : acc
}, []); return a
.concat(
[zipWith(
}).map( function (linea, ib) {
return a + b
}, [0].concat(lstPreviousRow),
lstPreviousRow.concat(0)
)]
pairSums(lst );
}, [[1]]);
}
 
 
// Next line in a Pascal triangle series
 
// [n] --> [n]
// GENERIC FUNCTIONS
function nextPascal(lst) {
 
return lst.length ? [1].concat(
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
pairSums(lst)
function pairSumszipWith(lstf, xs, ys) {
).concat(1) : [1];
return lstxs.length ?=== [1]ys.concatlength ? (
xs.map(function (x, i) {
return f(x, ys[i]);
}),
) : undefined;
}
 
// Eachrange row:: isInt a-> functionInt of-> the preceding row[Int]
function pascalTrianglerange(m, n) {
return n ? Array.apply(null, Array(n - 1)).reduce(
function return Array.apply(anull, _,Array(n i)- {m + 1))
return a .concatmap([nextPascalfunction (a[x, i])]); {
}, [[1]]) : [] return m + i;
});
}
 
// TEST
var lstTriangle = pascalTrianglepascal(n);
 
 
// FORMAT OUTPUT AS WIKI TABLE
 
// [[a]] -> bool -> s -> s
function wikiTable(lstRows, blnHeaderRow, strStyle) {
return '{| class="wikitable" ' + (
strStyle ? 'style="' + strStyle + '"' : ''
) + lstRows.map(function (lstRow, iRow) {
var strDelim = ((blnHeaderRow && !iRow) ? '!' : '|');
 
return '\n|-\n' + strDelim + ' ' + lstRow.map(function (v) {
return typeof v === 'undefined' ? ' ' : v;) {
return typeof v === 'undefined' ? ' ' : v;
}).join(' ' + strDelim + strDelim + ' ');
})
}).join('') + '\n|}';
}) .join(' ' + strDelim + strDelim + ' ');
}
false, })
}) .join('') + '\n|}';
}
 
var lstLastLine = lstTriangle.slice(-1)[0],
lngBase = (lstLastLine.length * 2) - 1,
nWidth = lstLastLine.reduce(function (a, x) {
var d = x.toString().length;
return d > a ? d : a .length;
}, 1) * lngBase return d > a ? d : a;
}, 1) * lngBase;
 
return [
wikiTable(
lstTriangle.map(function (lst) {
return lst.join(';;').split(';');
.split(';');
}).map(function (line, i) {
var lstPad = Array((lngBase - line.length) / 2});
return lstPad .concatmap(function (line, i).concat(lstPad); {
var lstPad = Array((lngBase - line.length) / 2);
}),
return lstPad.concat(line)
false,
.concat(lstPad);
'text-align:center;width:' + nWidth + 'em;height:' + nWidth +
}),
'em;table-layout:fixed;'
false,
'text-align:center;width:' + nWidth + 'em;height:' + nWidth +
'em;table-layout:fixed;'
),
 
9,659

edits