grangerelixx

grangerelixx

Passing list values to a function

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! :smiley:

Most Liked

derek-zhou

derek-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 :ok is just a place holder for the return value. In Elixir there is no void function. You are welcomed to discard the meaningless return value.

derek-zhou

derek-zhou

If you want side effect only, then use Enum.each/2

Last Post!

APB9785

APB9785

Creator of ECSx

In elixir, everything is “discarded” except the last line of the function. For example:

def my_func(param1, param2) do
  Database.query(param1) # returns list of users [%User{} ...]
  UserProfile.update!(param1, param2) # returns %UserProfile{} struct
  1 + 1
end

The only thing my_func is going to return is 2. 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 of my_func. Only the last line is returned. If you want to return the result of several function in combination, you use the pipeline operator:

def my_func(user_id, new_email) do
  User.get_user!(user_id)
  |> User.update!(new_email)
  |> Map.get(:email)
end

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.

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54996 245
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement