Dusty

Dusty

Passing an updated struct to each iteration of Enum

I am trying to iterate over a map, and repeatedly update a struct using each {k, v} pair in the map. But I am having difficulty figuring out how to pass the new/updated struct to the next iteration of the function. So I have something like:

struct = %MyStruct{...}
map = %{key1: value1, key2: value2,...}

def my_func(struct, key, value) do
  struct = # some code that updates the struct using the key and value
  struct # return the new struct
end

and I am trying to do something like:


struct = Enum.each(map, fn {key, value} -> my_func(struct, key, value) end)
{:ok, struct}

Obviously that code does not work, because Enum.each/2 returns :ok, but doesn’t carry the updated struct into the next iteration of the function, and doesn’t return the struct itself. It seems like I need a form of Enum.reduce/3 where the struct acts as the accumulator. How would you approach this?

Most Liked

henrik

henrik

Hi Dusty!

You could indeed use Enum.reduce/3:

iex> fake_struct = %{x: 0}
%{x: 0}
iex> map = %{key1: 1, key2: 2}
%{key1: 1, key2: 2}
iex> Enum.reduce(map, fake_struct, fn ({_k, v}, s) -> %{s | x: s.x + v} end)
%{x: 3}
Dusty

Dusty

@henrik @jmurphyweb

Thank you both for your time. It appears I just need a broader mental definition of an accumulator. Examples of reduce that I’ve seen always seem to use simple structures (like integer values) as an accumulator, but if the struct itself can be the accumulator then I think I’m in good shape.

jmurphyweb

jmurphyweb

Usually you would reduce over the map, and use a struct as the accumulator.

Enum.reduce(map, struct, fn {key, val}, new_struct -> 
  # ... perform some logic
  %{new_struct | key: value}
end)

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement