Skip to content

Instantly share code, notes, and snippets.

@petros
Last active August 28, 2022 22:32
Show Gist options
  • Save petros/7cbaacb697de6a52c1931703dd92039e to your computer and use it in GitHub Desktop.
Save petros/7cbaacb697de6a52c1931703dd92039e to your computer and use it in GitHub Desktop.
RPN Calculator - Elixir - Exercism
defmodule RPNCalculator do
@spec calculate!(list(), Function.t()) :: :ok
def calculate!(_stack, operation) do
operation.(1)
end
@spec calculate(list(), Function.t()) :: {:ok, String.t()} | :error
def calculate(_stack, operation) do
try do
operation.(1)
{:ok, "operation completed"}
rescue
_ -> :error
end
end
@spec calculate_verbose(list(), Function.t()) :: {:ok | :error, String.t()}
def calculate_verbose(_stack, operation) do
try do
operation.(1)
{:ok, "operation completed"}
rescue
e in ArgumentError -> {:error, e.message}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment