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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
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
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Most Liked- 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:LostKobrakai
If you really want to use
foryou can. Since a few versions back it also supports reductions.stefanchrobot
See also: https://elixir-lang.org/getting-started/case-cond-and-if.html#if-and-unless
Last Post!
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: