Note: Piping arithmetic in Elixir

April 20, 2024
elixir exercism

For an exercism exercise I wanted to do multiplication with the pipe operator, and simply using * does not work, neither does (*).

Now, it turns out the arithmetic operations are functions part of the Kernel module, and you can also call them as an actual function with Kernel.*(2, 2), which is equal to writing 2 * 2.

This enables you do do arithmetic as part of the pipe operation, see:

def monthly_rate(hourly_rate, discount) do
  hourly_rate
  |> daily_rate
  |> apply_discount(discount)
  |> Kernel.*(22)
  |> ceil()
end
Embrace the Challenge of Learning: Insights from Andrej Karpathy Shorter feedback loops with Elixir tests