Skip to content

Instantly share code, notes, and snippets.

@jeffweiss
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save jeffweiss/3a8a417dd1d6caa2e4d2 to your computer and use it in GitHub Desktop.

Select an option

Save jeffweiss/3a8a417dd1d6caa2e4d2 to your computer and use it in GitHub Desktop.

Revisions

  1. Jeff Weiss revised this gist Apr 28, 2015. 4 changed files with 71 additions and 2 deletions.
    15 changes: 15 additions & 0 deletions basic_bench.exs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    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
    13 changes: 13 additions & 0 deletions benchfella results
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    $ 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
    41 changes: 41 additions & 0 deletions num_to_words_list.ex
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    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
    4 changes: 2 additions & 2 deletions num_to_words.exs → num_to_words_string.ex
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    defmodule NumToWords do
    defmodule NumToWordsString do
    def say(0), do: "zero"
    def say(number) when is_integer(number), do: to_word(number) |> String.strip
    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)}"
  2. Jeff Weiss revised this gist Apr 28, 2015. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions num_to_words.exs
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,9 @@
    defmodule NumToWords do
    def say(0), do: "zero"
    def say(number) when is_integer(number), do: to_word(number) |> 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"
    @@ -34,4 +36,6 @@ defmodule NumToWords do
    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
  3. Jeff Weiss created this gist Apr 28, 2015.
    37 changes: 37 additions & 0 deletions num_to_words.exs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    defmodule NumToWords do
    def say(number) when is_integer(number), do: to_word(number) |> String.strip
    def say(_), do: IO.puts "That's not a number"

    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))}"
    end