Logo

Programming-Idioms

History of Idiom 37 > diff from v20 to v21

Edit summary for version 21 by programming-idioms.org:
[Elixir] +DemoURL

Version 20

2016-11-13, 23:10:20

Version 21

2017-02-07, 22:40:35

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
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
Origin
http://blog.patrikstorm.com/function-currying-in-elixir
Demo URL
http://play.elixirbyexample.com/s/15ef070434