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 range(1, n - 1)
return foldl(function (a) {
.reduce(function (a) {
var xs = a.slice(-1)[0]; // Previous row
var lstPreviousRow = a.slice(-1)[0];
return append(a, [zipWith(
function (a, b) {
return a + b;
},
append([0], xs),
append(xs, [0])
)]);
}, [
[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 ------------------------------------------------------

// (++) :: [a] -> [a] -> [a]
function append(xs, ys) {
return xs.concat(ys);
};

// enumFromTo :: Int -> Int -> [Int]
function enumFromTo(m, n) {
return Array.from({
length: Math.floor(n - m) + 1
}, function (_, i) {
return m + i;
});
};


// 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 ? b : a;
};


// 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 xs.length === ys.length ? (
return Array.from({
xs.map(function (x, i) {
length: min(xs.length, ys.length)
return f(x, ys[i]);
}, function (_, 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);
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 +
strStyle ? 'style="' + strStyle + '"' : ''
'"' : '') + lstRows.map(function (lstRow, iRow) {
) + lstRows.map(function (lstRow, iRow) {
var strDelim = blnHeaderRow && !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 = (lstLastLine.length * 2) - 1,
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) {
return lst.join(';;')
wikiTable(
lstTriangle.map(function (lst) {
.split(';');
return lst.join(';;')
})
.split(';');
.map(function (line, i) {
})
var lstPad = Array((lngBase - line.length) / 2);
.map(function (line, i) {
return lstPad.concat(line)
var lstPad = Array((lngBase - line.length) / 2);
.concat(lstPad);
}), 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>
})(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===