demerphq

demerphq

AssignWhen - use when clauses on assignment statements

Recently I was in a conversation with José Valim where I happened to mention that I find myself writing code like this:

X = if COND, do: Y, else: X

and that I wished I had some way to avoid the else boilerplate and make the code simpler. I asked him if there was some way I could write something like this:

X = Y IFF COND

He hummed and hawed for a minute and then pasted me the macro code that is now in the assign_when package on hex. This allows you to use when on assignment statements like this:

X = Y when COND

which is exactly equivalent to

X = if COND, do: Y, else: X

It works with tuples, and anything where you can say X = X. So for instance

{a,b} = {b,a} when a > b

should work fine. IMO avoiding the boilerplate like this makes for more elegant and more readable code. YMMV.

I want to repeat that I take no credit for this, the only thing I did was put release his hack to hex so people can use it. I personally think it would be a great addition to the language as a whole, but I am content to use it via a macro in this package. It uses some weird tricks that seem to cause my more experienced elixir colleagues to marvel over it, and I should note that it seems to confuse the ex_docs automagic on the docs page, but it works nicely so far for me. Thank you José!

https://github.com/demerphq/assign_when

Where Next?

Popular in Announcing Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
tmbb
I’ve published the first version of my Makeup library. It’s a syntax highlighter for Elixir in the spirit of Pygments, Currently it highl...
New
anshuman23
Hello all, I have been working on my proposed project called Tensorflex as part of Google Summer of Code 2018.. Tensorflex can be used f...
New
RobertDober
Earmark is a pure-Elixir Markdown converter. It is intended to be used as a library (just call Earmark.as_html), but can also be used as...
239 12888 134
New
mindok
What is ContEx? A pure Elixir server-side data plotting/charting library outputting SVG. It has nice barcharts in particular and works g...
New
brainlid
LangChain is short for Language Chain. An LLM, or Large Language Model, is the “Language” part. This library makes it easier for Elixir a...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement