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

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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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

Other popular topics Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement