JupiterIO1

JupiterIO1

Update value in map based on Enum.member? check

Ultimately I’d like to update the values of keys in myMap based on whether the key is in myList and in myOtherList:

myList = ["a", "b", "c"]
myOtherList = ["b"]
myMap = %{"a" => "no", "b" => "no", "c" => "no"}

Enum.each(myList, fn e -> 
    case Enum.member?(myOtherList, e) do
        true -> Map.replace(myMap, e, "yes")
        false -> Map.replace(myMap, e, "no")
    end
end)

myMap doesn’t update.

I believe the problem would be because of immutability, but I still cannot arrive to a solution. I’m open to other Map function suggestions.

Marked As Solved

JupiterIO1

JupiterIO1

The solution was solved in another topic:

Also Liked

peerreynders

peerreynders

Stop thinking of “variables”- all values in Elixir are immutable. However identifiers can be rebound to new values. So the only thing that can “vary” is the value the identifier is bound to - not the value itself.
Imperative languages are about PLace-Oriented Programming (PLOP) - in functional programming you are programming with values (The Value of Values).

Furthermore you don’t “loop” in Elixir - you recurse.

jgonet

jgonet

Popcorn Core Team

I suggest using if else for this. Also, to mutate your map you have to use explicit assignment, so my_map = if (...).

Oh, and in Elixir snake_case is the most widely used convention.

sanswork

sanswork

Check out Enum.reduce

Last Post!

JupiterIO1

JupiterIO1

You are right. As much as I have learnt and understood this concept, I should be more specific with my language. ty

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 131117 1222
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement