Function currying in Elixir
Currying is a technique that translates a function of arity N into a sequence of functions that all have the arity 1. Function arity means the number of arguments a function takes.
# The add function has the arity of 2 add = fn x, y -> x + y end
The term curry/currying was coined back in the 60s referring to the logician Haskell Curry. The programming language named after him has curried functions built in, and many other languages have implemented the same kind of behavior via libraries.
Implementing currying in Elixir #
Elixir is a functional language, but unlike Haskell, it does not have built in function currying.
In Haskell we could do this
let add x y = x + y -- add is curried by default let increment = add 1 increment 3 -- returns 4
A naive approach #
Elixir functions are first class, so your function can return them and receive them as values.
add = fn x ->
Continue reading →