From ccf73d62156e8ec2014828072a3ca34698427f69 Mon Sep 17 00:00:00 2001 From: Brian Hicks Date: Thu, 29 Oct 2020 09:52:14 -0500 Subject: [PATCH] remove duplication --- src/League.elm | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/src/League.elm b/src/League.elm index 574bd7d..96e64ae 100644 --- a/src/League.elm +++ b/src/League.elm @@ -163,30 +163,23 @@ nextMatch (League league) = case allPlayers of -- at least two a :: b :: rest -> - (case List.filter (\player -> player.matches <= playInMatches) allPlayers of - firstPlayIn :: restOfPlayIns -> - let - mostPlayed = - (firstPlayIn :: restOfPlayIns) - |> List.map .matches - |> List.maximum - |> Maybe.withDefault firstPlayIn.matches - in - Random.weighted - ( toFloat (mostPlayed - firstPlayIn.matches), firstPlayIn ) - (List.map (\player -> ( toFloat (mostPlayed - player.matches), player )) restOfPlayIns) - - _ -> - let - mostMatches = - List.map .matches allPlayers - |> List.maximum - |> Maybe.withDefault 0 - in - Random.weighted - ( toFloat (mostMatches - a.matches) ^ 2, a ) - (List.map (\player -> ( toFloat (mostMatches - a.matches) ^ 2, player )) (b :: rest)) - ) + let + ( firstPossiblePlayer, restOfPossiblePlayers ) = + case List.filter (\player -> player.matches <= playInMatches) allPlayers of + [] -> + ( a, b :: rest ) + + firstPlayIn :: restOfPlayIns -> + ( firstPlayIn, restOfPlayIns ) + + mostMatchesAmongPossiblePlayers = + List.map .matches (firstPossiblePlayer :: restOfPossiblePlayers) + |> List.maximum + |> Maybe.withDefault firstPossiblePlayer.matches + in + Random.weighted + ( toFloat (mostMatchesAmongPossiblePlayers - firstPossiblePlayer.matches) ^ 2, firstPossiblePlayer ) + (List.map (\player -> ( toFloat (mostMatchesAmongPossiblePlayers - player.matches) ^ 2, player )) restOfPossiblePlayers) |> Random.andThen (\firstPlayer -> let