kalindkaria
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.
Trending in Chat/Questions
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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:stefanchrobot
See also: https://elixir-lang.org/getting-started/case-cond-and-if.html#if-and-unless
kalindkaria
Thanks for your reply.
But in my case, the
forloop control or thenis controlled from some other function and I need to update thesensor_datavariable based on the value ofn.I did try using
Enum.reducebefore but they can’t with an externalforloop as it takes the control of iteratingnfrom0..9.LostKobrakai
If you really want to use
foryou can. Since a few versions back it also supports reductions.kokolegorille
Not only, You also need the previous value.
That’s why You need the acc of a reducer…
kalindkaria
Thanks @LostKobrakai and @kokolegorille !
I missed out this
:reduceoption in Comprehensions. I am able to recall now.Documentation about
:reducefor anyone to refer in future: