2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,63 +1,133 @@
(function (lines) {
(function (strText) {
'use strict';
var LEFT = 0,
CENTRE = 1,
RIGHT = 2;
// [[a]] -> [[a]]
function transpose(lst) {
return lst[0].map(function (_, iCol) {
return lst.map(function (row) {
return row[iCol];
})
});
}
return alignedTable(
lines.map(function (s) {
return s.split('$');
// (a -> b -> c) -> [a] -> [b] -> [c]
function zipWith(f, xs, ys) {
return xs.length === ys.length ? (
xs.map(function (x, i) {
return f(x, ys[i]);
})
) : undefined;
}
// (a -> a -> Ordering) -> [a] -> a
function maximumBy(f, xs) {
return xs.reduce(function (a, x) {
return a === undefined ? x : (
f(x) > f(a) ? x : a
);
}, undefined)
}
// [String] -> String
function widest(lst) {
return maximumBy(length, lst)
.length;
}
// [[a]] -> [[a]]
function fullRow(lst, n) {
return lst.concat(Array.apply(null, Array(n - lst.length))
.map(function () {
return ''
}));
}
// String -> Int -> String
function nreps(s, n) {
var o = '';
if (n < 1) return o;
while (n > 1) {
if (n & 1) o += s;
n >>= 1;
s += s;
}
return o + s;
}
// [String] -> String
function unwords(xs) {
return xs.join(' ');
}
// [String] -> String
function unlines(xs) {
return xs.join('\n');
}
// [a] -> Int
function length(xs) {
return xs.length;
}
// -- Int -> [String] -> [[String]]
function padWords(n, lstWords, eAlign) {
return lstWords.map(function (w) {
var lngPad = n - w.length;
return (
(eAlign === eCenter) ? (function () {
var lngHalf = Math.floor(lngPad / 2);
return [
nreps(' ', lngHalf), w,
nreps(' ', lngPad - lngHalf)
];
})() : (eAlign === eLeft) ?
['', w, nreps(' ', lngPad)] :
[nreps(' ', lngPad), w, '']
)
.join('');
});
}
// MAIN
var eLeft = -1,
eCenter = 0,
eRight = 1;
var lstRows = strText.split('\n')
.map(function (x) {
return x.split('$');
}),
2, // minimum gap between cols
LEFT // [LEFT|CENTRE|RIGHT] or [0|1|2]
)
// TABULATION OF RESULTS IN SPACED AND ALIGNED COLUMNS
// [s] -> n -> enum -> s
function alignedTable(lstRows, lngPad, iAlignment) {
lngCols = widest(lstRows),
lstCols = transpose(lstRows.map(function (r) {
return fullRow(r, lngCols)
})),
lstColWidths = lstCols.map(widest);
// Max width of each column
var lstColWidths = range(0, lstRows.reduce(function (a, x) {
return x.length > a ? x.length : a;
}, 0) - 1).map(function (iCol) {
return lstRows.reduce(function (a, lst) {
var w = lst[iCol] ? lst[iCol].toString().length : 0;
return (w > a) ? w : a;
}, 0);
});
// THREE PARAGRAPHS, WITH VARIOUS WORD COLUMN ALIGNMENTS:
// Rows padded to equal width
return lstRows.map(function (lstRow) {
return lstRow.map(function (v, i) {
return align(v, lstColWidths[i] + lngPad, iAlignment);
}).join('')
}).join('\n');
}
return [eLeft, eRight, eCenter]
.map(function (eAlign) {
var fPad = function (n, lstWords) {
return padWords(n, lstWords, eAlign);
};
// Text padded to left and/or right: aligned (left|centre|right)
// String, number of characters, alignment 0|1|2
// s -> n -> enum -> s
function align(s, n, a) {
var mid = (1 === a),
gap = n - s.length,
pad = Array(Math.floor((gap + 1) / (mid ? 2 : 1))).join(' ');
return transpose(
zipWith(fPad, lstColWidths, lstCols)
)
.map(unwords);
})
.map(unlines)
.join('\n\n');
return (a ? pad + (!mid || gap % 2 ? '' : ' ') : '') + s +
(2 > a ? pad + (mid ? ' ' : '') : '');
}
// [m..n]
function range(m, n) {
return Array.apply(null, Array(n - m + 1)).map(function (x, i) {
return m + i;
});
}
})([
"Given$a$text$file$of$many$lines,$where$fields$within$a$line$",
"are$delineated$by$a$single$'dollar'$character,$write$a$program",
"that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$",
"column$are$separated$by$at$least$one$space.",
"Further,$allow$for$each$word$in$a$column$to$be$either$left$",
"justified,$right$justified,$or$center$justified$within$its$column."
]);
})(
"Given$a$text$file$of$many$lines,$where$fields$within$a$line$\n\
are$delineated$by$a$single$'dollar'$character,$write$a$program\n\
that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$\n\
column$are$separated$by$at$least$one$space.\n\
Further,$allow$for$each$word$in$a$column$to$be$either$left$\n\
justified,$right$justified,$or$center$justified$within$its$column."
);