Created
December 3, 2015 22:00
-
-
Save acadavid/1bdeaa51beea5b0585bf to your computer and use it in GitHub Desktop.
Solution to StringsAndBinaries-4 (Programming Elixir)
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 MyString do | |
def calculate(to_calc) do | |
{f, op, s} = _calculate(to_calc) | |
apply(Kernel, op, [f, s]) | |
end | |
def _calculate(t, acc \\ []) # Default value for acc | |
def _calculate([ ?\s | t ], acc), do: _calculate(t, acc) # Scape spaces | |
def _calculate([h|t], acc) when h in [?+, ?-, ?/, ?*] do | |
{to_digit(acc), List.to_atom([h]), to_digit(_calculate(t))} | |
end | |
def _calculate([h|t], acc), do: _calculate(t, acc ++ [h]) | |
def _calculate([], acc), do: acc | |
defp to_digit(acc), do: String.to_integer(List.to_string(acc)) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment