Sanjibukai

Sanjibukai

Returning a constant with an anonymous function with the capture operator &

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

Most Liked

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.

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.

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.

Last Post!

lkuty

lkuty

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

Where Next?

Popular in Discussions Top

PragTob
Hey everyone, this has been on my mind for some time and I’d love your input on it! TLDR: I feel like maps are superioer for storing and...
New
Crowdhailer
I’ve been hearing much about the new formatter and it’s something I have been keen to try. I find examples buy far the most illuminating...
248 19740 150
New
pillaiindu
In django there is a cache framework backed by memcached. Rails also puts a lot of emphasis on caching, and even the idea of russian-doll...
New
acrolink
How does the two languages compare when it comes to server side application development? Any experiences or ideas? Thank you.
New
lorenzo
Hey everone! I created a prototype for my app using Nodejs for the api. But the framework I chose wasnt great (in general theresnt any g...
New
PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
cvkmohan
The upcoming Phoenix 1.6 release looks very interesting. Became a habit to watch the commits - and - what they are bringing in. phx.gen...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement