github / email

Curried Elixir

Elixir already supports partially applied functions thanks to the capture operator, and partial application is nice. But you know what's even better? Currying!

Let's say we define this function:

def my_fun(a, b, c) do
  a + b + c

What we would want is to have a curry/1 function that would allow us transforming this function into a curried function:

iex> curried_fun = curry(&my_fun/3)
iex> curried_fun.(1).(2).(3)
iex> add_42 = curried_fun.(40).(2)
iex> add_42.(10)

We're going to do this by wrapping my_fun in a chain of anonymous functions, each binding exactly one name:

curried_fun =
  fn (a) ->
    fn (b) ->
      fn (c) ->
        my_fun.(a, b, c)

The most (and only?) important thing we need to know in order to implement this curry function is the arity of the function we are given. We can use for this:

iex> x, y -> x * y end, :arity)
{:arity, 2}

Now that we have this, let's write this curry function!

defmodule Func do
  def curry(fun) when is_function(fun) do
    {:arity, arity} =, :arity)
    curry(fun, arity, [])

  def curry(fun, 0, []), do: fn -> fun.() end
  def curry(fun, 1, args), do: fn x -> apply(fun, Enum.reverse([x | args])) end
  def curry(fun, arity, args), do: fn x -> curry(fun, arity - 1, [x | args]) end

And that's it! Here's what happens when we're using curry:

iex> f = curry(&
#Function<...> # fn x -> curry(&, 1, [x]) end
iex> f.(1..3)
#Function<...> # fn x -> apply(&, Enum.reverse([x, 1..3])) end
iex> f.(&"i = #{&1}")
["i = 1", "i = 2", "i = 3"] # apply(&, [1..3, &"i = #{&1}"])

0-arity functions are a special case, as we don't want curry to apply them for us, i.e.:

iex> curry(&self/0)
#PID<...> # apply(&self/0, [])

Instead, we wrap the function in a 0-arity anonymous function:

iex> f = curry(&self/0)
#Function<...> # fn -> apply(&self/0, []) end
iex> f.()
#PID<...> # apply(&self/0, [])