7stud

7stud

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?

Showing Posts 1 to 10

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:

7stud

7stud OP

Thanks for the response.

A relevant tag would just be ‘elixir’ or so,

There is no elixir tag shown in the drop down list. And, when I select a tag in error, and I want to remove the tag, I try to click on the the ‘x’ in the tag, but that doesn’t work (in Safari 11.1).

OvermindDL1

OvermindDL1

@AstonJ will need to look in to that, but do you get any error message in the javascript console or any error server responses in the network inspector?

7stud

7stud OP

Yeah, that doesn’t do what Ruby’s Hash.new does when you specify a block (or Perl’s autovivification). The idea is that you can treat the value returned by a non-existent key the same way you do an existing key. What’s lacking is that Map.udpate/4 does not pass the default value to the function, which I think is a terrible oversight:

If key is not present in map , initial is inserted as the value of key . The initial value will not be passed through the update function

Here’s what Map.update/4 does:

iex(5)> data = %{a: [1, 2], b: [3]}
%{a: [1, 2], b: [3]}

iex(6)> Map.update(data, :c, [], &([10|&1]))
%{a: [1, 2], b: [3], c: []}

iex(7)> Map.update(data, :a, [], &([10|1]))
%{a: [10, 1, 2], b: [3]}

I think Map.update/4 should create the key c: [10] on line 6.

Here is what you can do in Ruby:

2.4.0 :011 => h = Hash.new {|hash, key| hash[key] = []}
=> {}

2.4.0 :012> h[:a] = [1, 2]
=> [1, 2]

2.4.0 :013> h
=> {:a=>[1, 2]}

2.4.0 :014> h[:a] << 10
=> [1, 2, 10]

2.4.0 :015> h[:b] << 10
=> [10]

2.4.0 :016 >; h
=> {:a=>[1, 2, 10], :b=>10]}

See how convenient that is? Perl also allows you to do something similar.

OvermindDL1

OvermindDL1

I wish it did that as well, but you can always make your own function do it. :slight_smile:

For note, I’ve barely touched Ruby, I find it a very difficult to reason about language due to everything being in flux and so very untyped, so I don’t know what the defaults do, so overall it is better to explain what you want it to do rather than just say ‘this other language thing’ where not everyone knows that other language. ^.^;

I agree, I’ve always found that to be really weird, but I’ve chalked up a lot of Elixir’s weirdness to Ruby, guess it is the opposite this time. ^.^

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:

peerreynders

peerreynders

Something else to ponder:

iex(1)> data = Map.new()
%{}
iex(2)> Map.fetch(data, :some_key)
:error
iex(3)> some_key = Access.key(:some_key, [])
#Function<4.27061494/3 in Access.key/2>
iex(4)> some_cons = fn (data, item) ->
...(4)>   update_in(data, [some_key], &([item|&1]))
...(4)> end
#Function<12.127694169/2 in :erl_eval.expr/5>
iex(5)> data = data |> some_cons.(:some_data) |> some_cons.(:more_data)
%{some_key: [:more_data, :some_data]}
iex(6)> Map.fetch(data, :some_key)
{:ok, [:more_data, :some_data]}
iex(7)> 

Kernel.update_in/3
Access.key/2

7stud

7stud OP

Here are the javascript errors I get when I load the elixir forum page:

[Error] XMLHttpRequest cannot load https://forum.elixirforum.com/message-bus/28cbf0220fed43f297d5f0b8e70b0b09/poll due to access control checks.
	send (ember_jquery-27e777857b8c0730dacfe09cb11711365d21a5db4f9ee0b85d494e4259cf6cda.js:3:16695)
	ajax (ember_jquery-27e777857b8c0730dacfe09cb11711365d21a5db4f9ee0b85d494e4259cf6cda.js:3:14212)
	n (vendor-6e59f8d0190b766ab492d48c83983f67bb7a5ecbff9e213f79fa12e4c72a06e6.js:1:19242)
	(anonymous function) (vendor-6e59f8d0190b766ab492d48c83983f67bb7a5ecbff9e213f79fa12e4c72a06e6.js:1:18300)
[Error] XMLHttpRequest cannot load https://forum.elixirforum.com/message-bus/28cbf0220fed43f297d5f0b8e70b0b09/poll due to access control checks.
	send (ember_jquery-27e777857b8c0730dacfe09cb11711365d21a5db4f9ee0b85d494e4259cf6cda.js:3:16695)
	ajax (ember_jquery-27e777857b8c0730dacfe09cb11711365d21a5db4f9ee0b85d494e4259cf6cda.js:3:14212)
	n (vendor-6e59f8d0190b766ab492d48c83983f67bb7a5ecbff9e213f79fa12e4c72a06e6.js:1:19242)
	(anonymous function) (vendor-6e59f8d0190b766ab492d48c83983f67bb7a5ecbff9e213f79fa12e4c72a06e6.js:1:18300)
[Error] Failed to load resource: A server with the specified hostname could not be found. (analytics.js, line 0)

I don’t get any additional js errors when I try to “x out” a tag.

We generally don’t use ‘elixir’ as a tag since pretty much everything is related to Elixir on this forum

But the editor makes me choose at least one tag, and elixir is not a choice. So, should I choose the Ecto tag when my question has nothing to do with Ecto?

OvermindDL1

OvermindDL1

Ahh, okay that works!

I’m wondering if something more generic like ‘language implementation’ or ‘stdlib’ might be good?

AstonJ

AstonJ

The tags displayed change as you type and if the tag you want doesn’t exist, you can create it. So just tag a thread with what the thread is about :slight_smile:

The language-implementation tag already exists and stdlib exists as standard-library :slight_smile:

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

sntran
In my third year of college, around 2010, I found Erlang. A few years later, I found Elixir. I liked the ideas at once: many small proces...
New
GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews