Elm:折叠多个列表的特定值

Elm: Fold specific values of multiple lists

games =
    [ { id = 1
      , points =
            [ { player_id = 1, score = 20 }
            , { player_id = 2, score = 10 }
            , { player_id = 3, score = 0 }
            ]
      }
    , { id = 2
      , points =
            [ { player_id = 1, score = 20 }
            , { player_id = 2, score = 5 }
            , { player_id = 3, score = 0 }
            ]
      }
    , { id = 3
      , points =
            [ { player_id = 1, score = 20 }
            , { player_id = 2, score = 5 }
            , { player_id = 3, score = 10 }
            ]
      }
    ]

我有一个 'games' 记录列表,每个记录都有每个玩家的积分列表。 是否可以 fold 每个玩家在每场比赛中的每个分数都显示 'total'.

类似于:

[ { player_id = 1, score = 60 }
, { player_id = 2, score = 20 }
, { player_id = 3, score = 10 }
]

提前致谢。

你需要双眼皮,关键代码是

main : Html msg
main =
    let
        go { points } acc =
            let
                goInner { player_id, score } accInner =
                    Dict.update player_id (updater score) accInner
            in
            List.foldl goInner acc points
    in
    List.foldl go Dict.empty games
        |> toString
        |> text

updater : Int -> Maybe Int -> Maybe Int 
updater  score existingVal =  
    existingVal
        |> Maybe.withDefault 0 
        |> (+) score
        |> Just

参见 [https://ellie-app.com/gq8VFfS8Ja1/0](this 艾莉) 尽管格式略有不同,这将为您提供所需的答案