7stud

7stud

Updating a map's values that are lists

What is the Elixir equivalent to Ruby’s Hash.new where you can specify a block that returns an empty list when you access a non-existent key (or Perl’s autovivification feature)?

The idea is that when you access a key in a Map that doesn’t exist you get back an empty list, which you can then append values to. And, if the key does exist, then you get back a non-empty list, which you can also append values to.

Also, why is it that the editor for this forum has a textbox that says “choose optional tags for this topic”, yet when I submit my question, I get an alert that says “You must choose at least 1 tag”, yet none of the tags are relevant to my question?

First Post! Switch mode

OvermindDL1

OvermindDL1

In functional languages that kind of access is ‘inverted’, you specify the default arguments on the calls instead of the object, thus:

iex(1)> m = %{}
%{}
iex(2)> Map.get(m, :nope, [])
[]
iex(3)> Map.update(m, :nope, [6.28], &[ 6.28 |&1])
%{nope: [6.28]}

Which of course you can wrap up as new calls in your own custom module. :slight_smile:
Or do something generic like make a DefaultMap module that delegates to the normal Map module but sets an override default key in the map or something.
Lots of options. :slight_smile:

A relevant tag would just be ‘elixir’ or so, but adding tags is a good way to be able to search for topics in the future. :slight_smile:

Most Liked

sribe

sribe

Elixir is not Perl. The phrase “if you treat a value returned by a key as a list” makes no sense in the Elixir context–you’re basically saying “if you expect of type XXX and you get nil, then automagically create an XXX and substitute it”, and that is exactly the kind of implicit magic that Elixir tries to avoid, by design.

AstonJ

AstonJ

We generally don’t use ‘elixir’ as a tag since pretty much everything is related to Elixir on this forum :003: (and so I’ve made it a staff only tag)

I’ve added maps and lists as tags as they seem most relevant :023:

sribe

sribe

Don’t confuse syntactic sugar and implicit magic–the examples you post are all completely explicit.

Last Post!

Ryzey

Ryzey

The definition for Map.update is
def update(map, key, initial, fun) when is_function(fun, 1) where fun is a 1 arity function that receives the existing value.

I don’t think it would be a breaking change to add this definition:
def update(map, key, initial, fun) when is_function(fun, 2) where fun is a 2 arity function that receives the existing value and the initial value. By initial value, I mean the value that the user supplied as the 3rd argument.

The second definition is what I expected the functionality of update to be and what I would prefer (and I think this is what @7stud and @OvermindDL1 would prefer). Importantly, there would be no impact for anyone who did not want to use the initial value in fun. They could simply continue to provide a 1 arity function as fun.

As @overmindDL1 points out, there are options to work around this limitation, but I agree with @7stud that this is an oversight.

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
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
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement