Skip to content

Instantly share code, notes, and snippets.

@dkarter
Last active January 6, 2016 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkarter/4dd85b321dabe2bdc949 to your computer and use it in GitHub Desktop.
Save dkarter/4dd85b321dabe2bdc949 to your computer and use it in GitHub Desktop.
Round Robin Elm
module RoundRobin where
import Graphics.Element exposing (show)
import Set
import Array
type alias Team = String
type alias TeamMatch = (Team, Team)
removeRedundentMatches : List TeamMatch -> List TeamMatch
removeRedundentMatches allMatches =
let
reverseMatch match = (snd match, fst match)
isReverseInList match matches =
List.member (reverseMatch match) matches
isSameTeamMatch (teamA, teamB) =
teamA == teamB
isValidMatch match matches =
not (isSameTeamMatch match) && not (isReverseInList match matches)
filterReverseMatches match matches =
if isValidMatch match matches then
-- this line can potentially be improved
Array.toList <| Array.push match (Array.fromList matches)
else
matches
in
List.foldr filterReverseMatches [] allMatches
mapMatch : List Team -> Team -> List TeamMatch
mapMatch teamList team =
List.map ((,) team) teamList
matches : List Team -> List TeamMatch
matches teamList =
teamList
|> List.map (mapMatch teamList)
|> List.concat
|> removeRedundentMatches
main =
show (toString <| matches ["A", "B", "C", "D"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment