Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Elixir

Idiom #37 Currying

Transform a function that takes multiple arguments into a function for which some of the arguments are preset.

defmodule Curry do

  def curry(fun) do
    {_, arity} = :erlang.fun_info(fun, :arity)
    curry(fun, arity, [])
  end

  def curry(fun, 0, arguments) do
    apply(fun, Enum.reverse arguments)
  end

  def curry(fun, arity, arguments) do
    fn arg -> curry(fun, arity - 1, [arg | arguments]) end
  end

end
(def add5 (partial + 5))

New implementation...
< >
Adrian