Logo

Programming-Idioms

History of Idiom 37 > diff from v18 to v19

Edit summary for version 19 by barroco:
New Elixir implementation by user [barroco]

Version 18

2016-04-07, 07:40:28

Version 19

2016-08-23, 11:02:03

Idiom #37 Currying

Technique of transforming a function that takes multiple arguments and returning a function for which some of the arguments are preset.

Idiom #37 Currying

Technique of transforming a function that takes multiple arguments and returning a function for which some of the arguments are preset.

Code
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
Origin
http://blog.patrikstorm.com/function-currying-in-elixir