grangerelixx
I have a list of maps.
For example,
students = [%{ sid: 1, name: "Bob", score: 80}, %{ sid: 2, name: "Kelly", score: 60},.....]
I have a function that accepts a single student map as a parameter,
calculate_percentage(student) do
something
end
I want to be able to pass each map in students list to calculate_percentage function. I tried Enum.map but that returns a list again. Can I know how to pass each value in a list one by one to the calculate_percentage function? Any suggestions are appreciated.
Thanks! ![]()
Trending in Questions
One of my LiveView view’s has a very heavy javascript dependency so I want to avoid loading it for every page on the site, while still us...
New
Hi all!
I know that quite a few people use Phoenix LiveView but it is hard to get a more up-to-date list of folks/companies using it. I ...
New
Hello everyone!
I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++.
Does anyone ...
New
I’m very new to Elixir, so sorry if this is a silly question:
I am wondering what happens to the mailbox of a process when the machine i...
New
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
I don’t suppose you can lazy load a library depending on runtime data? For example if it’s a certain user they can lazy load ‘library A’ ...
New
Hi all,
I’m trying to get Pi working well as an external agent inside Tidewave.ai via ACP.
Tidewave’s settings make it possible to add ...
New
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
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hi everyone :waving_hand:
I’ve been working on PhiaUI, an open-source UI component library for Phoenix LiveView, and I wanted to share i...
New
On November 23, 2026, an unauthorized third party exploited a vulnerability in hex.pm (CVE-2026-129514, CVSS 10.0) that let them run comm...
New
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
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
- #code-sync
- #exunit
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #ai
- #authentication
- #elixirconf-us
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-eu
- #distillery
- #security
- #processes
- #api
- #forms
- #hex










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Nicd
You did not specify what the
calculate_percentagefunction should do and that is quite important.But if you want it to return a single value, you want to look at
Enum.reduce. A reduce is one of the most fundamental operations in functional programming so it’s useful to learn it. With it, you can reduce an input value to an output value. For example you can reduce a list into a single value (but the output can be anything, and it can even be a list).Without knowing what your function does, you probably want something like:
The accumulator is the value that has been accumulated during the processing of the input and in every call, you need to return the updated value of the accumulator, which will be given with the next value from the input. The final value of the accumulator is the return value of the reduce.
If this does not answer your question, can you elaborate on what
calculate_percentageshould do?derek-zhou
If you want side effect only, then use
Enum.each/2grangerelixx
I thought giving a simplified example would help. But the original list consists of a list of structs. And the I have written a function that accepts a single struct.
a single struct looks like this
So a list consists of
This is the function I have
Now, I want to be able to pass each struct in the list to this function.
grangerelixx
No I don’t want the side effect. Is there a way to use
Enum.eachwithout getting:ok?Enum.eachworks perfect for my requirement except for the fact that it returns:okderek-zhou
I think you are confused about what is side-effect. A function converts inputs to outputs. If the function does only this and nothing else, it is called a pure function, or a function without side effect. A side effect is something you want to achieve in addition to the conversion: Writing databases, sending some data to the network, etc.
If you don’t want side effect and don’t care about the return value, then there is really no point calling the function.
The
:okis just a place holder for the return value. In Elixir there is no void function. You are welcomed to discard the meaningless return value.APB9785
If you don’t want the return values because you are in a function pipeline, you can use the new Kernel - tap/2, introduced in Elixir 1.12
grangerelixx
Thank you for explaining this. When I use
Enum.each(posts, &IO.inspect/1), I get the followingI am not sure how to discard this
:ok. Any thoughts on how we can discard:ok?derek-zhou
Just don’t assign it to any variable?
The REPL is a exploration vehicle; not your program. When you run your program for real you will have the full control of what to print and what not to print.
APB9785
In elixir, everything is “discarded” except the last line of the function. For example:
The only thing
my_funcis going to return is2. The first two function calls will still happen - leading to potential “side_effects” (like updating a row in the database), but they have no effect on the return value ofmy_func. Only the last line is returned. If you want to return the result of several function in combination, you use the pipeline operator:This function will fetch a User from the db, pass that user along to the next function where it gets updated in the db, and finally get the email from out of our updated %User{} struct.