czrpb

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

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 :slight_smile:

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"]>
}

Also Liked

kokolegorille

kokolegorille

At least here I prefer … MapSet.new(value) :slight_smile:

kokolegorille

kokolegorille

That applies here too.

You could use…

|> Map.new()

…instead

al2o3cr

al2o3cr

Could alternatively be spelled:

update_in acc, [Access.key(key, MapSet.new())], &MapSet.put(&1, get_value_to_add(key))
ityonemo

ityonemo

There’s a limit to how much I’m willing to let my code look like it’s object oriented :sweat_smile:

kokolegorille

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 :slight_smile:

Where Next?

Popular in Questions Top

Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
minhajuddin
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
dotdotdotPaul
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
Tee
can someone please explain to me how Enum.reduce works with maps
New
pmjoe
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
LegitStack
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
pgiesin
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
gonzofish
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
Exadra37
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
vrod
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 Top

senggen
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
yurko
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
pmjoe
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
malloryerik
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
vac
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
shahryarjb
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
ovidiubadita
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
Fl4m3Ph03n1x
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
aesmail
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
joeerl
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

We're in Beta

About us Mission Statement