kalindkaria

kalindkaria

Map.update inside for loop

Hi, I’ve just started learning Elixir.
For one of the applications I’m developing, I have the following code:

use Bitwise
sensor_data = %{sensor1: 0}
for n <- 0..9, do: sensor_data = Map.update!(sensor_data, :sensor1, &(&1 ||| (1 <<< n)))

This produces following output:

[
  %{sensor1: 1},
  %{sensor1: 2},
  %{sensor1: 4},
  %{sensor1: 8},
  %{sensor1: 16},
  %{sensor1: 32},
  %{sensor1: 64},
  %{sensor1: 128},
  %{sensor1: 256},
  %{sensor1: 512}
]

But the expected output should be:

[
  %{sensor1: 1},
  %{sensor1: 3},
  %{sensor1: 7},
  %{sensor1: 15},
  %{sensor1: 31},
  %{sensor1: 63},
  %{sensor1: 127},
  %{sensor1: 255},
  %{sensor1: 511},
  %{sensor1: 1023}
]

If I run the following statement in iex session the sensor_data map gets updated like this:

sensor_data = Map.update!(sensor_data, :sensor1, &(&1 ||| (1 <<< 0)))
%{sensor1: 1}
sensor_data = Map.update!(sensor_data, :sensor1, &(&1 ||| (1 <<< 1)))
%{sensor1: 3}
sensor_data = Map.update!(sensor_data, :sensor1, &(&1 ||| (1 <<< 2)))
%{sensor1: 5}

Can you help me understand why does the Map.update!/3 doesn’t work inside a for loop?
I also tried with Map.update/4 Map.replace!/3 but no luck.

Most Liked

stefanchrobot

stefanchrobot

The “assignment” inside the for loop has no effect, it creates a new variable on each loop iteration. I think you might be looking for Enum.reduce:

Enum.reduce(0..9, %{sensor1: 0}, fn n, sensor_data ->
  Map.update!(sensor_data, :sensor1, &(&1 ||| (1 <<< n)))
end)
LostKobrakai

LostKobrakai

If you really want to use for you can. Since a few versions back it also supports reductions.

use Bitwise
sensor_data = %{sensor1: 0}
new_sensor_data = 
  for n <- 0..9, reduce: sensor_data do
    acc -> Map.update!(acc, :sensor1, &(&1 ||| (1 <<< n)))
  end
stefanchrobot

stefanchrobot

See also: https://elixir-lang.org/getting-started/case-cond-and-if.html#if-and-unless

This is also a good opportunity to talk about variable scoping in Elixir. If any variable is declared or changed inside if, case, and similar constructs, the declaration and change will only be visible inside the construct. For example:

iex> x = 1
1
iex> if true do
...>   x = x + 1
...> end
2
iex> x
1

In said cases, if you want to change a value, you must return the value from the if:

iex> x = 1
1
iex> x = if true do
...>   x + 1
...> else
...>   x
...> end
2

Last Post!

kalindkaria

kalindkaria

Thanks @LostKobrakai and @kokolegorille !
I missed out this :reduce option in Comprehensions. I am able to recall now.
Documentation about :reduce for anyone to refer in future:

Where Next?

Popular in Chat/Questions Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
332 31238 154
New
AstonJ
It’s been a while since we asked this - I’m sure others (especially newcomers to the language) will be interested to hear how you’ve all ...
New
asfand
I am Asfandyar from Pakistan. This is my first time to the forum. I develop PHP websites using CodeIgniter, which is super easy to learn...
New
Fl4m3Ph03n1x
Background I am trying to do the typical Ceaser cypher exercise in Elixir. The description of the exercise is as follows: Create an impl...
New
Santheepkumar
Hi all, I am a Fullstack JS developer for last 2 years. I need a good guide to learn elixer. Any suggestions please
New
dopomecana
The title of this topic may sound silly at the first glance. But why I trying to ask you guys is how to go to the next level? I am curre...
New
ariandanim
Hello all, I am still learning Elixir, then go into Phoenix, i am try search in google but find the programming phoenix 1.4, another for...
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
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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