Anshul-13J
Hey everyone, I am new to Elixir language and I am having some issues while writing a piece of code.
What I am given is a 2D array like
list1 = [
[1 ,2,3,4,"nil"],
[6,7,8,9,10,],
[11,"nil",13,"nil",15],
[16,17,"nil",19,20] ]
Now, what I’ve to do is to get all the elements that have values between 10 and 20, so what I’m doing is:
final_list = []
Enum.each(list1, fn row ->
Enum.each(row, &(if (&1 >= 10 and &1 <= 99) do final_list = final_list ++ &1 end))
end
)
Doing this, I’m expecting that I’ll get my list of numbers in final_list but I’m getting blank final list with a warning like:
warning: variable "final_list" is unused (there is a variable with the same name in the context, use the pin operator (^) to match on it or prefix this variable with underscore if it is not meant to be used)
iex:5
:ok
and upon printing final_list, it is not updated.
When I try to check whether my code is working properly or not, using IO.puts as:
iex(5)> Enum.each(list1, fn row -> ...(5)> Enum.each(row, &(if (&1 >= 10 and &1 <= 99) do IO.puts(final_list ++ &1) end))
...(5)> end
...(5)> )
The Output is:
10
11
13
15
16
17
19
20
:ok
What could I possibly be doing wrong here? Shouldn’t it add the elements to the final_list?
If this is wrong ( probably it is), what should be the possible solution to this?
Any kind of help will be appreciated.
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
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
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
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
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
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance










First 10 of 12 Posts
arcyfelix
In your specific example, it is better to use Enum.reduce.
Anshul-13J
On it! giving it a try
kokolegorille
Hello and welcome,
The code You try to write is not going to work in FP, for many reasons… mostly because data is immutable, and scope is strict. What is defined in a do end block will disappear when the block end.
It’s not an array, it’s a list of list, more prcisely they are linked lists, and do not have the same properties (Not done for index access)
The final list is not going to leak into the wrapping code.
Using Enum.each is procedural
What You should do
but because You don’t want nil, You could…
or better, as mentionned…
You need to use other tools, like the Enum module
Anshul-13J
Gave it a try but still not working!
Anshul-13J
Thanks for your valuable reply, looks like I gotta lot to learn!
Trying to implement it again
Anshul-13J
Tried this too:
Still getting the same error
kokolegorille
You might often realize that You could solve something like this with only one command.
This would be my solution.
You can see that You don’t need to initially set something to , You can set it in the function call.
It uses an anonymous function with multiple head, and guard clause, no need to use if.
You append an element to the list with [el | acc]
You can minimiize local variables with the use of pipe.
Oh, my code is only for 1 D, for 2 D I need to nest…
There is another tool, You might use in FP, it’s a zipper.
dpreston
From your initial question I think you are missing a key difference between Python and Elixir. Elixir variables are immutable and you cannot modify them like you would in Python.
incorrectly translated to Elixir (similar to your initial attempt)
instead should be written
The difference is that
Enum.reducebuilds up the accumulator array and then assigns it all toresultsat the end, it doesn’t modifyresultsas it processes each element of the list.In fact, it cannot modify
resultseven if we wanted to.The full solution then looks like this,
If you don’t need to retain the nested structure or process the elements further, I would suggest a different approach. First flatten the inner lists and then filter with a simple boolean test.
dimitarvp
Banging your head against the wall is a slow way to learn, dude. Trust me I know, I’ve done the same way too many times.
You have to assign the result of almost every function to a variable in Elixir because it does NOT change a variable in place like in Python in many others; it returns a modified copy of the variable.
List is still
[]in the end because the internal expressionlist = list ++ (x*2)is staying there and not going anywhere.If you run the above you’ll get this message:
It’s telling you that you are throwing away
listfrom inside theEnum.eachblock.The way you would achieve the desired result is this:
This goes through each element of the list, modifies it, and appends it to a resulting list.
TL;DR forget about using
Enum.eachorforto modify variables. They are not used for that, only for side effects (like printing to terminal, writing to files or external network services etc.)Anshul-13J
Really helpful one!
The thing is, I am understanding the code, but having issues in writing it! The syntax is a bit changed from languages like Cpp and Python, but I guess I’ll get there. Just need some practice!