Skip to content

Instantly share code, notes, and snippets.

@thiagokokada
Last active August 15, 2018 02:15
Show Gist options
  • Save thiagokokada/6060400f7555e513755d2efbdd1c1c49 to your computer and use it in GitHub Desktop.
Save thiagokokada/6060400f7555e513755d2efbdd1c1c49 to your computer and use it in GitHub Desktop.
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