Search code examples
elixircurrying

Is there standard curry() function in Elixir?


I want to apply a function partially. Is there standard concise way to do any kind of currying in Elixir?

I know I can do somethings like this:

new_func = fn(arg2, arg3) -> my_func(constant, arg2, arg3) end

new_func2 = fn -> my_func2(constant) end

but it looks ugly.


Solution

  • You can use the capture & operator to clean it up a bit:

    plus2 = &Kernel.+(2, &1)
    plus2.(4)
    6
    

    Notice the dot . in between plus2 and it's parens

    Since this is kind of syntactic sugar for

    plus2 = fn(right) -> Kernel.+(2, right) end
    

    all the same rules apply. Like you must supply all arguments to the function you're "currying" and you can place the positional arguments in any order.

    Docs on the & operator