Sanjibukai

Sanjibukai

Hello,

You may be already using the Capture Operator & for capturing functions or quickly creating anonymous function.

For this later case, you may also know that it’s not possible to use it for creating an anonymous function that return a constant or a literal or in other words without using a placeholder in the capture expression as explained in the doc:

The only restrictions when creating anonymous functions is that at least one placeholder must be present, i.e. it must contain at least &1 , and that block expressions are not supported:

# No placeholder, fails to compile.
&(:foo)

# Block expression, fails to compile.
&(&1; &2)

However it might be useful sometimes to return an atom or a constant or any literal.
Well you can bypass the parameter using || (the or operator):

&(:foo || &1)
&(true || &1)
&(42 || &1)

And if you want to return no matter what false or nil, well you can do it too, just use && (the and operator):

&(false || &1)
&(nil || &1)

Have a good day everyone..

Showing Posts 1 to 8

lkuty

lkuty

Why not but I think that, most of the time, if I have to write &(:foo || &1), I might as well write fn -> :foo end. Maybe in some syntactic context, &(...) is more readable than fn -> ... end. I don’t know, I guess it depends on the use case. Anyway, thanks for sharing.

Sanjibukai

Sanjibukai OP

I completely agree.
I find it useful just for visual consistency when I’m using &(...) in all the other clauses.
But as you implicitly said, in fact in this case it might be less readable regarding the intent.
So yeah.. Might stick with the fn _ -> :foo end

dimitarvp

dimitarvp

These will never return &1 so I really don’t see the point.

lucaong

lucaong

If I understand correctly, it’s a trick to use the capture operator even when one does not want to use any of the placeholders, so it is intentional that &1 is never used. It is there just because otherwise the anonymous function would not compile.

If on one hand I like the creative thinking around that, I would avoid this as it is confusing. As already stated, fn -> :foo end is much clearer and almost as short.

More generally, after an initial enthusiasm, I switched to almost never use the capture operator for anonymous functions. I find fn -> ... end a lot clearer, and saving a few characters not worth. On the other hand, I find the capture operator for named functions (like &Module.function/3) very useful.

Sanjibukai

Sanjibukai OP

Indeed it’s exactly that.

And at the end of the day I agree with you too.
As I answered to @lkuty it’s correct that this little trick might be indeed more “tricky” and hide some intent..
Exactly like how @dimitarvp fell in the trap.. It appears that without focus the code is more obscure and the intent become indeed hidden.

As you know and can saw on some google discussion it’s not possible to capture with 0 arity and I had like the author of that thread some use-case candidate.

Sanjibukai

Sanjibukai OP

Sorry if I was confusing..
But I thought that with the very sentence right above this code snippet (and in fact all the explanation of the entire post) that the goal to return not the first argument &1 but precisely the constant was clear..

So you just proved that this indeed lacks of readability.

axelson

axelson

Scenic Core Team

For others reading I wanted to note that fn _ -> :foo is the equivalent of &(:foo || &1) (not fn -> :foo). Depending on how you’re using the anonymous function the different arities could make a significant difference. There is no way to create a 0-arity anonymous function using the & capture syntax.

lkuty

lkuty

Yes, that’s also why I prefer the thunk style fn -> :foo end.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
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
GES233
I’m posting this in response to Jose’s recent tweet (Cr. link) : People are sleeping on Elixir for a coding harness: Hot-code swappi...
New
_mfierro
Hello, I wrote Stop My Hand, a Scattergories-like web application using Phoenix/LiveView as my learning project for Elixir (after readin...
New
marciol
It would be helpful to have a list of companies worldwide that hire engineers without prior experience in Elixir. Often, it can be quite ...
New
durvia
Anyone running long-lived stateful processes on BEAM? We’re building an AI agent runtime and would love to compare notes. We’re a small ...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews