22 lines
399 B
Elixir
22 lines
399 B
Elixir
defmodule RPNCalculator do
|
|
def calculate!(stack, operation) do
|
|
operation.(stack)
|
|
end
|
|
|
|
def calculate(stack, operation) do
|
|
try do
|
|
{:ok, operation.(stack)}
|
|
rescue
|
|
_ in RuntimeError -> :error
|
|
end
|
|
end
|
|
|
|
def calculate_verbose(stack, operation) do
|
|
try do
|
|
{:ok, operation.(stack)}
|
|
rescue
|
|
e in ArgumentError -> {:error, e.message}
|
|
end
|
|
end
|
|
end
|