czrpb
ARRG! Still stuck in the imperative world! Help! :)
Have a Map whose values are MapSets: Best way to update?
Currently have:
keys
|> Enum.reduce(
%{},
fn key, acc ->
acc
|> Map.put(
key,
Map.get(acc, key, MapSet.new())
|> MapSet.put(get_value_to_add(key))
)
end
)
Marked As Solved
kokolegorille
Except the MapSet…
iex> list = ["apple", "apricot", "banana", "blueberry", "kiwi", "blackberry"]
["apple", "apricot", "banana", "blueberry", "kiwi", "blackberry"]
iex> Enum.group_by(list, &String.first/1)
%{
"a" => ["apple", "apricot"],
"b" => ["banana", "blueberry", "blackberry"],
"k" => ["kiwi"]
}
With MapSet…
list
|> Enum.group_by(&String.first/1)
|> Enum.reduce(%{}, fn {k, v}, acc -> Map.put(acc, k, MapSet.new(v)) end)
%{
"a" => #MapSet<["apple", "apricot"]>,
"b" => #MapSet<["banana", "blackberry", "blueberry"]>,
"k" => #MapSet<["kiwi"]>
}
With MapSet, short version 
iex> list
|> Enum.group_by(&String.first/1)
|> Enum.reduce(%{}, &Map.put(&2, elem(&1, 0), MapSet.new(elem(&1, 1))))
%{
"a" => #MapSet<["apple", "apricot"]>,
"b" => #MapSet<["banana", "blackberry", "blueberry"]>,
"k" => #MapSet<["kiwi"]>
}
2
Also Liked
kokolegorille
At least here I prefer … MapSet.new(value) ![]()
4
kokolegorille
That applies here too.
You could use…
|> Map.new()
…instead
4
al2o3cr
Could alternatively be spelled:
update_in acc, [Access.key(key, MapSet.new())], &MapSet.put(&1, get_value_to_add(key))
3
ityonemo
There’s a limit to how much I’m willing to let my code look like it’s object oriented 
3
kokolegorille
It is not very clear what You want to achieve… better put some data and expected result.
Anyway, this looks wrong (bad parens placement…)
Map.get(acc, key, MapSet.new())
|> MapSet.put(get_value_to_add(key))
and piping once is something I try not to do. At least two pipes, otherwise I pass the parameter normally.
There is a Map.update function ![]()
2
Popular in Questions
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
can someone please explain to me how Enum.reduce works with maps
New
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it.
I’m very interested in Elixir,...
New
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database.
Dep...
New
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
Sometimes I want to check if the input into a function is not a blank string.
My first approach:
defmodule Example do
def do_stuff(s...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New
Other popular topics
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
Hi,
I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
Hello guys,
I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New








