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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With