Last active
August 29, 2015 14:20
-
-
Save jeffweiss/3a8a417dd1d6caa2e4d2 to your computer and use it in GitHub Desktop.
Numbers to words
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 BasicBench do | |
use Benchfella | |
@num :random.uniform(999_999_999_999) | |
bench "interpolation" do | |
@num | |
|> NumToWordsString.say | |
end | |
bench "list" do | |
@num | |
|> NumToWordsList.say | |
end | |
end |
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
$ mix bench | |
Settings: | |
duration: 1.0 s | |
## BasicBench | |
[15:03:21] 0/2: list | |
[15:03:24] 0/2: interpolation | |
Finished in 4.74 seconds | |
## BasicBench | |
list 500000 4.45 µs/op | |
interpolation 50000 32.73 µs/op |
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 NumToWordsList do | |
def say(0), do: "zero" | |
def say(number) when is_integer(number), do: [to_word(number)] |> List.flatten |> Enum.reject(&Kernel.is_nil/1) |> Enum.join(" ") | |
def say(_), do: IO.puts "That's not a number" | |
defp to_word(n) when n < 0, do: ["negative", to_word(-n)] | |
defp to_word(0), do: nil | |
defp to_word(1), do: "one" | |
defp to_word(2), do: "two" | |
defp to_word(3), do: "three" | |
defp to_word(4), do: "four" | |
defp to_word(5), do: "five" | |
defp to_word(6), do: "six" | |
defp to_word(7), do: "seven" | |
defp to_word(8), do: "eight" | |
defp to_word(9), do: "nine" | |
defp to_word(10), do: "ten" | |
defp to_word(11), do: "eleven" | |
defp to_word(12), do: "twelve" | |
defp to_word(13), do: "thirteen" | |
defp to_word(14), do: "fourteen" | |
defp to_word(15), do: "fifteen" | |
defp to_word(16), do: "sixteen" | |
defp to_word(17), do: "seventeen" | |
defp to_word(18), do: "eighteen" | |
defp to_word(19), do: "nineteen" | |
defp to_word(20), do: "twenty" | |
defp to_word(30), do: "thirty" | |
defp to_word(40), do: "forty" | |
defp to_word(50), do: "fifty" | |
defp to_word(60), do: "sixty" | |
defp to_word(70), do: "seventy" | |
defp to_word(80), do: "eighty" | |
defp to_word(90), do: "ninety" | |
defp to_word(n) when n < 100, do: [to_word(div(n,10)*10), to_word(rem(n, 10))] | |
defp to_word(n) when n < 1_000, do: [to_word(div(n,100)), "hundred", to_word(rem(n, 100))] | |
defp to_word(n) when n < 1_000_000, do: [to_word(div(n,1_000)), "thousand", to_word(rem(n, 1_000))] | |
defp to_word(n) when n < 1_000_000_000, do: [to_word(div(n,1_000_000)), "million", to_word(rem(n, 1_000_000))] | |
defp to_word(n) when n < 1_000_000_000_000, do: [to_word(div(n,1_000_000_000)), "billion", to_word(rem(n, 1_000_000_000))] | |
defp to_word(_), do: "I'm sorry, I can't count that high." | |
end |
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 NumToWordsString do | |
def say(0), do: "zero" | |
def say(number) when is_integer(number), do: to_word(number) |> String.replace(~r/\s+/, " ") |> String.strip | |
def say(_), do: IO.puts "That's not a number" | |
defp to_word(n) when n < 0, do: "negative #{to_word(-n)}" | |
defp to_word(0), do: "" | |
defp to_word(1), do: "one" | |
defp to_word(2), do: "two" | |
defp to_word(3), do: "three" | |
defp to_word(4), do: "four" | |
defp to_word(5), do: "five" | |
defp to_word(6), do: "six" | |
defp to_word(7), do: "seven" | |
defp to_word(8), do: "eight" | |
defp to_word(9), do: "nine" | |
defp to_word(10), do: "ten" | |
defp to_word(11), do: "eleven" | |
defp to_word(12), do: "twelve" | |
defp to_word(13), do: "thirteen" | |
defp to_word(14), do: "fourteen" | |
defp to_word(15), do: "fifteen" | |
defp to_word(16), do: "sixteen" | |
defp to_word(17), do: "seventeen" | |
defp to_word(18), do: "eighteen" | |
defp to_word(19), do: "nineteen" | |
defp to_word(20), do: "twenty" | |
defp to_word(30), do: "thirty" | |
defp to_word(40), do: "forty" | |
defp to_word(50), do: "fifty" | |
defp to_word(60), do: "sixty" | |
defp to_word(70), do: "seventy" | |
defp to_word(80), do: "eighty" | |
defp to_word(90), do: "ninety" | |
defp to_word(n) when n < 100, do: "#{to_word(div(n,10)*10)} #{to_word(rem(n, 10))}" | |
defp to_word(n) when n < 1_000, do: "#{to_word(div(n,100))} hundred #{to_word(rem(n, 100))}" | |
defp to_word(n) when n < 1_000_000, do: "#{to_word(div(n,1_000))} thousand #{to_word(rem(n, 1_000))}" | |
defp to_word(n) when n < 1_000_000_000, do: "#{to_word(div(n,1_000_000))} million #{to_word(rem(n, 1_000_000))}" | |
defp to_word(n) when n < 1_000_000_000_000, do: "#{to_word(div(n,1_000_000_000))} billion #{to_word(rem(n, 1_000_000_000))}" | |
defp to_word(_), do: "I'm sorry, I can't count that high." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment