Multiplication tables: Difference between revisions

Content added Content deleted
Line 2,040: Line 2,040:


<lang JavaScript>(function (m, n) {
<lang JavaScript>(function (m, n) {

// [m..n]
// [m..n]
function range(m, n) {
function range(m, n) {
Line 2,047: Line 2,047:
});
});
}
}

// Monadic bind (chain) for lists
// Monadic bind (chain) for lists
function chain(xs, f) {
function mb(xs, f) {
return [].concat.apply([], xs.map(f));
return [].concat.apply([], xs.map(f));
}
}

var lstRange = range(m, n),
var rng = range(m, n),

lstTable = [['x'].concat(lstRange)].concat(
lstTable = [['x'].concat( rng )]
chain(lstRange, function (y) {
.concat(mb(rng, function (x) {
return [[y].concat(
return [[x].concat(mb(rng, function (y) {
chain(lstRange, function (x) {
return x < y ? [''] : [x * y]; // triangle only
return y < x ? [''] : [x * y]; // triangle only
})
)]
}))]}));
})
);


/* FORMATTING OUTPUT */
/* FORMATTING OUTPUT */

// [[a]] -> bool -> s -> s
// [[a]] -> bool -> s -> s
function wikiTable(lstRows, blnHeaderRow, strStyle) {
function wikiTable(lstRows, blnHeaderRow, strStyle) {
Line 2,074: Line 2,071:
) + 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,080: Line 2,077:
}).join('') + '\n|}';
}).join('') + '\n|}';
}
}

// Formatted as WikiTable
// Formatted as WikiTable
return wikiTable(
return wikiTable(
Line 2,086: Line 2,083:
'text-align:center;width:33em;height:33em;table-layout:fixed;'
'text-align:center;width:33em;height:33em;table-layout:fixed;'
) + '\n\n' +
) + '\n\n' +
// or simply stringified as JSON
// or simply stringified as JSON
JSON.stringify(lstTable);
JSON.stringify(lstTable);

})(1, 12);</lang>
})(1, 12);</lang>