14 lines
517 B
Plaintext
14 lines
517 B
Plaintext
require! 'prelude-ls': {fold, sum}
|
|
|
|
# String → Number
|
|
decimal_of_roman = do
|
|
# [Number, Number] → String → [Number, Number]
|
|
_convert = ([acc, last_value], ch) ->
|
|
current_value = { M:1000 D:500 C:100 L:50 X:10 V:5 I:1 }[ch] ? 0
|
|
op = if last_value < current_value then (-) else (+)
|
|
[op(acc, last_value), current_value]
|
|
# fold the string and sum the resulting tuple (array)
|
|
fold(_convert, [0, 0]) >> sum
|
|
|
|
{[rom, decimal_of_roman rom] for rom in <[ MCMXC MMVII MDCLXVII MMMCLIX MCMLXXVII MMX ]>}
|