In Scala and some other functional languages you can create a lambda using placeholder syntax, like (_ + _)
means something like (_1,_2) -> _1 + _2
. Elixir uses syntax like &(&2 + &1)
for a quick lambda or &foo.bar
to create a bound method.
This seems handy for very small functions like this one, where you are basically creating a function from a simple expression.
I do wonder whether we're buying much here, given you're basically saving one character from x -> x + 1
vs -> $1 + 1
. And the version with a named parameter could have a meaningful named parameter, too. BUT if we're relying on these quick lambdas to support currying and bound methods, it might be worth it. Or if the syntax can be shortened even more.
It seems like these would almost always need to be wrapped in parentheses so perhaps a variation on parentheses would be good like &(&2.bla(&1))
means _1,_2->_2.bla(_1)