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.

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)
3 Likes

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
1 Like

Thanks for your reply.
But in my case, the for loop control or the n is controlled from some other function and I need to update the sensor_data variable based on the value of n.
I did try using Enum.reduce before but they can’t with an external for loop as it takes the control of iterating n from 0..9.

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
1 Like

Not only, You also need the previous value.

That’s why You need the acc of a reducer…

1 Like

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:
https://hexdocs.pm/elixir/Kernel.SpecialForms.html#for/1-the-reduce-option

1 Like