Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 964
raacer Avatar asked Dec 02 '16 00:12

raacer


People also ask

Is currying supported in scheme?

It is possible to generate curried functions in Scheme. The function curry2 generates a curried version of a function, which accepts two parameters. The curried version takes one parameter at a time. Similarly, curry3 generates a curried version of a function that takes three parameters.

What is meant by currying?

to praise someone, especially someone in authority, in a way that is not sincere, in order to get some advantage for yourself: He's always trying to curry favour with the boss.


1 Answers

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

like image 108
greggreg Avatar answered Nov 09 '22 13:11

greggreg