4 ms·
Not knowing Elixir at all, what's with all the & operators in your pipe example? Looks very unappealing to me.
by Spiritus 11y ago
Not knowing Elixir at all, what's with all the & operators in your pipe example? Looks very unappealing to me.
- Bockit 11y agoIt's the super terse anonymous function syntax. You could use the slightly more verbose version with named instead of positional params or put a named function in there instead.
- jeremyjh 11y agoThis is a shorthand for writing anonymous functions. The &() activates the feature for the expression inside, which can then use &1, &2 etc to apply parameters. For example `&(&1 + 1)` is equivalent to `fn x -> x + 1 end` .
- deleted 11y ago[deleted]
- lectrick 11y agoIt's a shorthand. Once you get tired of the long form, the shorthand will be appealing :P
- dwoot 11y agoWe call it the "capture" operator. What it does is it captures the nth parameter of the anonymous function. Like some have already said, the number just signifies which parameter you're making a reference to in the function body. It's terse and comes in handy for ad-hoc stuff. I prefer verbosity and often write Elixir code with the standard syntax and find myself using the capture operator a lot more in the REPL.