Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -0,0 +1,48 @@
|
|||
(function (lstTest) {
|
||||
|
||||
var mapping = [["M", 1000], ["CM", 900], ["D", 500], ["CD", 400], ["C", 100], [
|
||||
"XC", 90], ["L", 50], ["XL", 40], ["X", 10], ["IX", 9], ["V", 5], ["IV",
|
||||
4], ["I", 1]];
|
||||
|
||||
// s -> n
|
||||
function romanValue(s) {
|
||||
// recursion over list of characters
|
||||
// [c] -> n
|
||||
function toArabic(lst) {
|
||||
return lst.length ? function (xs) {
|
||||
var lstParse = chain(mapping, function (lstPair) {
|
||||
return isPrefixOf(
|
||||
lstPair[0], xs
|
||||
) ? [lstPair[1], drop(lstPair[0].length, xs)] : []
|
||||
});
|
||||
return lstParse[0] + toArabic(lstParse[1]);
|
||||
}(lst) : 0
|
||||
}
|
||||
return toArabic(s.split(''));
|
||||
}
|
||||
|
||||
// Monadic bind (chain) for lists
|
||||
function chain(xs, f) {
|
||||
return [].concat.apply([], xs.map(f));
|
||||
}
|
||||
|
||||
// [a] -> [a] -> Bool
|
||||
function isPrefixOf(lstFirst, lstSecond) {
|
||||
return lstFirst.length ? (
|
||||
lstSecond.length ?
|
||||
lstFirst[0] === lstSecond[0] && isPrefixOf(
|
||||
lstFirst.slice(1), lstSecond.slice(1)
|
||||
) : false
|
||||
) : true;
|
||||
}
|
||||
|
||||
// Int -> [a] -> [a]
|
||||
function drop(n, lst) {
|
||||
return n <= 0 ? lst : (
|
||||
lst.length ? drop(n - 1, lst.slice(1)) : []
|
||||
);
|
||||
}
|
||||
|
||||
return lstTest.map(romanValue);
|
||||
|
||||
})(['MCMXC', 'MDCLXVI', 'MMVIII']);
|
||||
|
|
@ -0,0 +1 @@
|
|||
[1990, 1666, 2008]
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
(function (lstTest) {
|
||||
|
||||
function romanValue(s) {
|
||||
return s.length ? function () {
|
||||
var parse = [].concat.apply([], glyphs.map(function (g) {
|
||||
return 0 === s.indexOf(g) ? [trans[g], s.substr(g.length)] : [];
|
||||
}));
|
||||
return parse[0] + romanValue(parse[1]);
|
||||
}() : 0;
|
||||
}
|
||||
|
||||
var trans = {
|
||||
M: 1E3,
|
||||
CM: 900,
|
||||
D: 500,
|
||||
CD: 400,
|
||||
C: 100,
|
||||
XC: 90,
|
||||
L: 50,
|
||||
XL: 40,
|
||||
X: 10,
|
||||
IX: 9,
|
||||
V: 5,
|
||||
IV: 4,
|
||||
I: 1
|
||||
},
|
||||
glyphs = Object.keys(trans);
|
||||
|
||||
return lstTest.map(romanValue);
|
||||
|
||||
})(["MCMXC", "MDCLXVI", "MMVIII", "MMMM"]);
|
||||
|
|
@ -0,0 +1 @@
|
|||
[1990, 1666, 2008]
|
||||
Loading…
Add table
Add a link
Reference in a new issue