# Day 20

## Exercises

* [x] [RPN Calculator](#rpn-calculator)

### RPN Calculator

[Exercise on Exercism](https://exercism.org/tracks/elixir/exercises/rpn-calculator) **|** [View my solution](https://exercism.org/tracks/elixir/exercises/rpn-calculator/solutions/petros)

#### Solution

<details>

<summary>Expand to see code (spoiler alert)</summary>

{% code lineNumbers="true" %}

```elixir
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

```

{% endcode %}

[View gist on GitHub](https://gist.github.com/petros/7cbaacb697de6a52c1931703dd92039e)

</details>

#### Notes

I was trying to figure out how to call an anonymous function assigned to a variable when the anonymous functions has no arguments. It's `the_variable.()`. However, it turns out this exercise expects an anonymous function with an arity of 1. But the parameter is not being used. So you can pass anything really: `the_variable.(1)`.

## Overall progress

<figure><img src="https://722931016-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDUq5E45vIeUDn4lY3Psn%2Fuploads%2FdhxspACXCeSN0lvRTMzP%2F20220827-exhort-day20-progress.png?alt=media&#x26;token=3964669b-2f7a-4eb3-a40b-e4b7624ad272" alt="An image showing my progress on Exercism. It&#x27;s 21.8% as of Saturday, 27 August 2022."><figcaption><p>Progress</p></figcaption></figure>
