JsonKody
What are the reasons behind Elixir's lambda syntax?
This is highly subjective but in my mind Haskell and Ruby/Rust has one of the most beautiful lambda syntax
\a b → a + b
and
|a, b| a + b
so if Elixir syntax is mostly form Ruby why they ditched one of the most beautiful things on Ruby and goes with this abomination monster:
&(&1 + &2)
??
![]()
PS: Please note, my intent is not to criticize; I’m genuinely interested in understanding the design choices behind this syntax. ![]()
edit/note: |a, b| a + b is just Rust syntax, I thought that it’s also Ruby, but while Ruby is similar it wraps it in labda { ... }
Most Liked
gpopides
I dont know the answer to your question but just a small note, this is not the lambda syntax.
The lambda syntax would be fn a, b -> a + b end
What you mention is the capture operator which is similar to method reference in java/kotlin
hauleth
Ruby lambda syntax is lambda { |a, b| a + b } which is far from pretty IMHO. You probably meant block syntax, but that is slightly different from lambdas, as you can have only one block, with that fancy pants syntax, per function call. So it is not fully apples to apples.
sodapopcan
Not to bikeshed (except that I’m gonna) but I do agree the &elem(&1, 2) is a little jarring even though I do use it sometimes
It’s a bit of a tough example for me to judge because I find elem/2 jarring ![]()
I do this kind of thing with abandon:
Enum.find(users, &(&1.name == "Bob"))
Enum.map(users, & &1.id)
I see users so &1 must be a user because even though naming is hard, it’s not that hard ![]()
Also, the odd time I need an identity function (almost always as a parameter default) I like & &1 as a nice little concise option, as in:
def foo(thing, mapper \\ & &1)
fn x -> x end just looks like someone made a mistake to me. Of course, the best option is likely &Function.indentity/1. You’re still using a capture there, but it’s the nice capture syntax and clear as day.
I do also do stuff like this:
update(socket, :users, &[user | &1])
I can’t explain it but that looks totally fine to me, again, so long as there is clear naming. It also certainly makes a difference if it’s used in a common pattern or not.
I work solo, though. I wouldn’t fight anyone too hard on any of this stuff in a team situation.
One thing I never use (anymore) is &2. &2 can heck off!
Last Post!
sodapopcan
I’m not opposed to significant-whitespace other than I do sometimes like the ability to outdent in development to draw my eye to something. Like a debug statement, for example. I don’t make use of this too often, but I don’t find end ugly either. I do (lol) like it in Elixir since it keeps with that lispy-feeling of the underlying syntax.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









