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

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
mikl
I wanted to capitalize a string, and tried using String.capitalize(). That generally works well, until you try to capitalize a word like...
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
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 39247 209
New
crabonature
I’m still quite new to Elixir. As I understand we got in Elixir “multi guards” as convention to simplify one large guard with or’s?: de...
New
New
jer
I’ve been using umbrellas for a while, and generally started off (on greenfield projects at least) by isolating subapps based on clearly ...
New
jsonify
So, is Heroku the only free option for hosting Phoenix/Elixir at this point? I’m not ready to commit to paying monthly and was wondering ...
New
und0ck3d
Hello everyone! A few days ago I’ve created a topic here about how people were creating CMSs with Elixir and Phoenix. I’ve been studying...
New
griffinbyatt
Sobelow Sobelow is a security-focused static analysis tool for the Phoenix framework. For security researchers, it is a useful tool for g...
New

Other popular topics Top

greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31107 143
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement