JsonKody

JsonKody

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)
??
:grin: :laughing: :sweat_smile:

PS: Please note, my intent is not to criticize; I’m genuinely interested in understanding the design choices behind this syntax. :grinning_face_with_smiling_eyes:

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 { ... }

Showing Posts 20 to 11

carterbryden

carterbryden

I find the &1, &2 syntax hard to read too, though I can also see why some people like it.

The fn args → … end syntax I find a bit odd too just because it’s the only place I know of that isn’t a do/end block.

sodapopcan

sodapopcan

That actually doesn’t look bad to me as you have it but I have never written something like that myself before where I extract the reducer with extra arg(s). In any event, I was mostly being silly—I grepped a recent codebase right after I posted to make sure I wasn’t lying about not ever using &2 :sweat_smile:—it’s more of an arbitrary line I draw for myself as & can definitely look janky to me in many cases. I imagine it’s one of those things that’s easier for teams to either say yes or no to.

dimitarvp

dimitarvp

You are right, there are degrees. Your examples I too don’t find bad.

But you are also right that elem/2 is jarring. :smiley:

krasenyp

krasenyp

I have used Enum.reduce(enumerable, initial, &reducer(extraarg, &1, &2) numerous times when I don’t want to use a lambda for whatever reasons. Maybe I’ve gotten used to it and I don’t find it hard to read.

sodapopcan

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 :grimacing: It’s a bit of a tough example for me to judge because I find elem/2 jarring :sweat_smile:

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 :upside_down_face:

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!

dimitarvp

dimitarvp

Yeah, it ruins readability.

D4no0

D4no0

This one baffles me too, I’m not sure what the thought process behind it was. This syntax is barely readable in isolation, not to talk in situations where there are multiple chained calls like this one.

dimitarvp

dimitarvp

Exactly the same and it’s also one of my top 5 reasons to leave it behind. That, plus it being so hopelessly dynamic to the point that even Elixir looks strict like Rust when compared to it.

Ruby remains a very friendly to look at language (and to write in, until you have to do something serious at least; cue 1000 angry comments saying “BUT SO MUCH SERIOUS STUFF IS DONE IN RUBY, YOU ARE WRONG!” :sweat_smile:) but its runtime properties are a complete “nah I am good” factor for me.


To the OP @JsonKody, I too find Rust’s syntax one of the best, and it leaves room for adding types too. OCaml is nice too.

And btw I kind of agree with him, I don’t think that the capture syntax with &1 and &2 etc. adds something tangible to the language. :person_shrugging:

Though there are two kinds of capture syntaxes. This one I like a lot:

Enum.map(list_of_strings, &String.trim/1)

…and this one I avoid like the plague most of the time:

Enum.map(list_of_tuples, &elem(&1, 2))

To me it just looks confusing and even after 7.5y with Elixir I still find it an eye-sore that prevents me to scan code well and comprehend it quickly. No idea why but I never got used to it.

Finally, this SO thread is good: https://stackoverflow.com/questions/44266193/why-do-we-need-a-function-capture-operator-in-elixir

sodapopcan

sodapopcan

By default Rubocop (at least the last time I used it) makes you write it like this: ->(a, b) { a + b } and the arrow must touch the parens! But if it spans multiple lines, it wants you do write it as lambda do ... end.

sodapopcan

sodapopcan

…and if you want to capture a block then it’s proc { ... }, or Proc.new { ... }!

I do still have a softspot for Ruby, but I wish it would have stopped adding syntax a long time ago. I happened to switch fully over to Elixir just as field punning was introduced to Ruby. Just in the nick of time!

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews