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

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews