dbulic

dbulic

Map merge shortcut

I would like to propose a syntax for merging maps.
Map can be updated via | operator, like %{map | key: value}
I believe it would be very useful to add a similar merge operator, like %{map || key: value, newkey: newvalue}

Most Liked

Qqwy

Qqwy

TypeCheck Core Team

Elixir already has Map.merge/2 and Map.merge/3. The latter allows to pass a function to resolve conflicts between the two maps.

Why do you believe that an infix operator would make maps more usable? Also, how would this be used with structs?

In my opinion, I do not find the new operator to enhance readability. I would prefer something like:

defmodule MapMerge do
  def a ||| b do
    Map.merge(a, b)
  end
end

import MapMerge
%{foo: 1, bar: 2} ||| %{bar: "overridden", baz: 4}

which is already possible without changing the core language.

16
Post #2
gregvaughn

gregvaughn

WOW! I did not know you could just def infix operators like that. I expected more macro-ish overhead. That’s great!

Qqwy

Qqwy

TypeCheck Core Team

Yes! This completely works. The only things you need to be aware of:

  1. Only a small subset of operators you can conceive are recognized as such by the compiler.
  2. Of these, most are already taken by Kernel. Only \\, <-, |, ~>>, <<~, ~>, <~, <~>, <|>, <<<, >>>, |||, &&&, and ^^^ are not used by default (some of these are used by the Bitwise module) and are user-overridable. (you can use the others, but then when someone uses your library they need to use e.g. import Kernel, except: [ +: 2])
  3. You cannot alter the operator precedence of an operator, this is set by the compiler. See the Operator Table to check what they are.
  4. Overloaded operators usually do not work in guard clauses, unless you build it as a macro using only Erlang BIFS. For most useful operations, this is impossible.
  5. … and finally, most syntax highlighters don’t highlight them properly yet :sweat_smile:

Oh, and an aside: all recognized operators are valid ways to start a symbol. So :<-> is fine, as is [&&&: 3].

Where Next?

Popular in Discussions Top

matthias_toepp
I’d love to hear what people think about Wisp, the new Gleam web framework started by Gleam’s primary creator Louis Pilfold. Gleam, alon...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39523 209
New
AstonJ
If a newbie asked you about Phoenix Contexts, how would you explain the basics to them? Feel free to be as concise or in-depth as you li...
New
Ankhers
Just a little information upfront. Generally speaking, if I feel like I need to either break a pipe chain or use an anonymous function in...
New
PragTob
Hey everyone, this has been brewing in my head some time and it came up again while reading Adopting Elixir. GenServers, supervisors et...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
AstonJ
If so I (and hopefully others!) might have some tips for you :slight_smile: But first, please say which area you’re finding most challen...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
sashaafm
Piggy backing a bit on @dvcrn topic BEAM optimization for functions with static return type?, I’ve been trying to understand in a deeper ...
New
Qqwy
Looking at the stacks that existing large companies have used, WhatsApp internally uses Mnesia to store the messages, while Discord uses ...
New

Other popular topics Top

WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36432 110
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

We're in Beta

About us Mission Statement