Last active
August 15, 2018 02:15
-
-
Save thiagokokada/6060400f7555e513755d2efbdd1c1c49 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Calculator do | |
def repl(acc \\ 0.0) do | |
IO.puts("accumulator> #{acc}") | |
[command, value] = IO.gets("operation> ") |> String.trim() |> String.split() | |
{value, _} = Float.parse(value) # Convert a string to its float representation | |
acc |> run_command(command, value) |> repl() | |
end | |
defp run_command(acc, command, value) do | |
case command do | |
"=" -> value | |
"+" -> acc + value | |
"-" -> acc - value | |
"*" -> acc * value | |
"/" -> acc / value | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment