Last active
June 15, 2016 20:07
-
-
Save devstopfix/c412104ff5746e5b54fbb522d65187c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule RomanNumerals do | |
# Roman Numerals (Elixir) | |
@digits %{"I" => 1, | |
"V" => 5, | |
"X" => 10, | |
"L" => 50, | |
"C" => 100, | |
"D" => 500, | |
"M" => 1000} | |
@doc """ | |
Integer value of a String of Roman Numerals. | |
""" | |
def value(s) do | |
String.codepoints(s) | |
|> Enum.map(fn d -> Map.get(@digits, d, 0) end) | |
|> Enum.chunk(2, 1, [0]) | |
|> Enum.reduce(0, &RomanNumerals.accumulate/2) | |
end | |
def accumulate([a, b], acc) do | |
if a >= b do | |
acc + a | |
else | |
acc - a | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With pattern matching: