Last active
January 6, 2016 23:04
-
-
Save dkarter/4dd85b321dabe2bdc949 to your computer and use it in GitHub Desktop.
Round Robin Elm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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