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 }
]
}
]
我有一个“游戏”记录列表,每个记录都有一个每个玩家的积分列表。
是否可以fold
每个游戏中每个玩家的每个分数显示“总数”。
类似的东西:
[ { player_id = 1, score = 60 }
, { player_id = 2, score = 20 }
, { player_id = 3, score = 10 }
]
提前致谢。
答案 0 :(得分:2)
您需要双折,其中密钥代码为
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 ellie) 这将为您提供您想要的答案,尽管格式略有不同