Immutability in Elixir is really blowing my mind and makes the languages so confusing to use. I need to iterate a nested map and simple update some counts based on the iteration, but Enum.reduce just makes it difficult for me. Say I have:

defmodule Predictor do def past_matches() do [ team1: %{team2: %{f: 0, a: 1}, team3: %{f: 1, a: 3}}, team2: %{team1: %{f: 3, a: 0}, team3: %{f: 2, a: 0}}, team3: %{team1: %{f: 1, a: 0}, team2: %{f: 0, a: 1}}, ] end def init_indexes(matches) do local = Enum.reduce matches, %{}, fn({team, _scores}, acc) -> Map.put acc, team, %{f: 0, a: 0, n_games: 0} end Enum.each matches, fn({team, scores}) -> Enum.each scores, fn({vteam, %{f: ff, a: aa}}) -> %{f: fi, a: ai, n_games: ni} = local[team] put_in(local[team], %{f: fi+ff, a: ai+aa, n_games: ni+1}) end end local end def run() do local = past_matches() |> init_indexes() end end

I need local to just sum f , a and n_games .

local = %{ team1: %{f: 1, a: 4, n_games: 2} ... }