grangerelixx

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

First 9 of 9 Posts Switch mode

Nicd

Nicd

You did not specify what the calculate_percentage function 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:

Enum.reduce(students, 0, fn student, accumulator ->
  calculate_percentage(accumulator, student)
end)

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_percentage should do?

derek-zhou

derek-zhou

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

grangerelixx

grangerelixx OP

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

%Post{
         id : uuid, 
         name: some name,
         feedbacks: [
                 %Comment{
                           ........
                         },

                 %Comment{
                           ........
                         }
                 ]
}

So a list consists of

[
%Post{
         id : uuid, 
         name: some name,
         feedbacks: [
                 %Comment{
                           ........
                         },

                 %Comment{
                           ........
                         }
                 ]
},
%Post{
         id : uuid, 
         name: some name,
         feedbacks: [
                 %Comment{
                           ........
                         },

                 %Comment{
                           ........
                         }
                 ]
},
%Post{
         id : uuid, 
         name: some name,
         feedbacks: [
                 %Comment{
                           ........
                         },

                 %Comment{
                           ........
                         }
                 ]
}
]

This is the function I have

def translate(%Post{} = post) do
    post
    |> Post.changeset(%{
      feedbacks:
        translate_feedbacks(
          post.feedbacks
        )
    })
    |> Ecto.Changeset.apply_changes()

Now, I want to be able to pass each struct in the list to this function.

grangerelixx

grangerelixx OP

No I don’t want the side effect. Is there a way to use Enum.each without getting :ok ? Enum.each works perfect for my requirement except for the fact that it returns :ok

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.

APB9785

APB9785

Creator of ECSx

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

grangerelixx OP

Thank you for explaining this. When I use Enum.each(posts, &IO.inspect/1), I get the following

%Post{
         id : uuid, 
         name: some name,
         feedbacks: [
                 %Comment{
                           ........
                         },

                 %Comment{
                           ........
                         }
                 ]
}
%Post{
         id : uuid, 
         name: some name,
         feedbacks: [
                 %Comment{
                           ........
                         },

                 %Comment{
                           ........
                         }
                 ]
}
%Post{
         id : uuid, 
         name: some name,
         feedbacks: [
                 %Comment{
                           ........
                         },

                 %Comment{
                           ........
                         }
                 ]
}
:ok

I am not sure how to discard this :ok. Any thoughts on how we can discard :ok?

derek-zhou

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

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
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
spammy
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
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
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
bottlenecked
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
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
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

We're in Beta

About us Mission Statement