2

I have list of map of orders that contain

%{price, order_details} 

I wanted to get the total_price in that orders and I also want to return and array of changeset that was build using the order_details of each element in that map. I can't find a way to do it without using one reduce and one map but that is two trip in to the list. it is that a good practice in functional programming?

1

2 Answers 2

5

I'm assuming the data looks like this:

list = [ %{price: 1, order_details: "foo"}, %{price: 2, order_details: "bar"}, %{price: 3, order_details: "baz"}, ] 

You can still use reduce with a tuple of sum and list as the accumulator.

list |> Enum.reduce({0, []}, fn x, {sum, list} -> {sum + x.price, [x.order_details | list]} end) |> IO.inspect 

Output:

{6, ["baz", "bar", "foo"]} 
Sign up to request clarification or add additional context in comments.

Comments

5

Also answer by @Dogbert is perfectly valid, there is a built-in for doing map-reduce in Elixir (Enum.map_reduce/3):

list = [ %{price: 1, order_details: "foo"}, %{price: 2, order_details: "bar"}, %{price: 3, order_details: "baz"}, ] Enum.map_reduce(list, 0, fn(%{order_details: od, price: p}, acc) -> {od, acc + p} # `od` is a mapper, `acc + p` is a reducer end) #⇒ {["foo", "bar", "baz"], 6} 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.