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
Hey folks, I’ve been using Elixir for over a couple of years (phx, ecto, broadway, oban).
For this kind of work you do not tend, in my e...
New
Hi, I’m Dheeraj Sudan from the UK. I’m a software developer and also run a business with my wife Meenu Hinduja. I’m interested in getting...
New
Other Trending Topics
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 6 of 6 Posts
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: