Skip to content

Instantly share code, notes, and snippets.

@erantapaa
Last active July 31, 2017 19:04
Show Gist options
  • Select an option

  • Save erantapaa/c8fa731e70aade1b555ee4340ae60634 to your computer and use it in GitHub Desktop.

Select an option

Save erantapaa/c8fa731e70aade1b555ee4340ae60634 to your computer and use it in GitHub Desktop.
pattern synonyms / views and regexs
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms, ViewPatterns #-}
{-# LANGUAGE QuasiQuotes, FlexibleContexts #-}
-- Example of using PatternSynonyms and ViewPatterns to simplify
-- the parsing of a text file with regexs.
module Heavy where
import Data.Monoid
import Control.Monad
import qualified Data.ByteString.Char8 as BS
import Control.Monad.State.Strict
import Control.Monad.Writer
import Data.Maybe
import Text.Regex.PCRE.Heavy
type Str = BS.ByteString
runRE re x = case scan re x of
[] -> Nothing
(a,as):_ -> Just (a:as)
pattern Comment <- (runRE [re|^\s*#|] -> Just _)
pattern MatchDay d <- (runRE [re|^\s*Matchday\s+(\d+)|] -> Just [_, toInt -> d])
pattern Blank <- (runRE [re|^\s*$|] -> Just _)
pattern Date dow month day <- (runRE [re|^\s*\[(\S+)\s+(\S+)/(\d+)\]|] -> Just [ _, dow, month, toInt -> day ])
pattern Score team1 score1 team2 score2 <- (runRE [re|^\s*([A-Z].*?\S)\s+(\d+)-(\d+)\s+(\S.*\S)|] -> Just [ _, team1, toInt -> score1, toInt -> score2, team2 ])
emit = BS.putStrLn
xemit _ = return ()
toInt :: Str -> Int
toInt x = case BS.readInt x of
Just (i, _) -> i
Nothing -> error "toInt failed"
path = "z-data.txt"
data Game = Game { _matchday :: Int
, _dow :: Str
, _month :: Str
, _day :: Int
, _team1 :: Str
, _team2 :: Str
, _score1 :: Int
, _score2 :: Int
}
deriving (Show)
initialGame = Game 0 "" "" 0 "" "" 0 0
main = do
contents <- BS.readFile path
let lines = BS.split '\n' contents
(_, games) <- evalStateT (runWriterT (mapM processLine lines)) initialGame
forM_ games $ \g -> emit (_team1 g)
processLine :: Str -> WriterT [Game] (StateT Game IO) ()
processLine line =
case line of
Comment -> return ()
Blank -> return ()
MatchDay m -> modify (\s -> s { _matchday = m })
Date dow mon day -> modify $ \s -> s { _dow = dow, _month = mon, _day = day }
Score t1 s1 t2 s2 -> do
modify $ \s -> s { _team1 = t1, _team2 = t2, _score1 = s1, _score2 = s2 }
get >>= (\g -> tell [g])
_ -> liftIO $ emit $ "Unknown: " <> line
####################################
# English Premier League 2015/16
Matchday 1
[Sat Aug/8]
Manchester United 1-0 Tottenham Hotspur
AFC Bournemouth 0-1 Aston Villa
Everton FC 2-2 Watford FC
Leicester City 4-2 Sunderland AFC
Norwich City 1-3 Crystal Palace
Chelsea FC 2-2 Swansea City
[Sun Aug/9]
Arsenal FC 0-2 West Ham United
Newcastle United 2-2 Southampton FC
Stoke City 0-1 Liverpool FC
[Mon Aug/10]
West Bromwich Albion 0-3 Manchester City
Matchday 2
[Fri Aug/14]
Aston Villa 0-1 Manchester United
[Sat Aug/15]
Southampton FC 0-3 Everton FC
Sunderland AFC 1-3 Norwich City
Swansea City 2-0 Newcastle United
Tottenham Hotspur 2-2 Stoke City
Watford FC 0-0 West Bromwich Albion
West Ham United 1-2 Leicester City
[Sun Aug/16]
Crystal Palace 1-2 Arsenal FC
Manchester City 3-0 Chelsea FC
[Mon Aug/17]
Liverpool FC 1-0 AFC Bournemouth
Matchday 3
[Sat Aug/22]
Manchester United 0-0 Newcastle United
Crystal Palace 2-1 Aston Villa
Leicester City 1-1 Tottenham Hotspur
Norwich City 1-1 Stoke City
Sunderland AFC 1-1 Swansea City
West Ham United 3-4 AFC Bournemouth
[Sun Aug/23]
West Bromwich Albion 2-3 Chelsea FC
Everton FC 0-2 Manchester City
Watford FC 0-0 Southampton FC
[Mon Aug/24]
Arsenal FC 0-0 Liverpool FC
Matchday 4
[Sat Aug/29]
Newcastle United 0-1 Arsenal FC
AFC Bournemouth 1-1 Leicester City
Aston Villa 2-2 Sunderland AFC
Chelsea FC 1-2 Crystal Palace
Liverpool FC 0-3 West Ham United
Manchester City 2-0 Watford FC
Stoke City 0-1 West Bromwich Albion
Tottenham Hotspur 0-0 Everton FC
[Sun Aug/30]
Southampton FC 3-0 Norwich City
Swansea City 2-1 Manchester United
Matchday 5
[Sat Sept/12]
Everton FC 3-1 Chelsea FC
Arsenal FC 2-0 Stoke City
Crystal Palace 0-1 Manchester City
Norwich City 3-1 AFC Bournemouth
Watford FC 1-0 Swansea City
West Bromwich Albion 0-0 Southampton FC
Manchester United 3-1 Liverpool FC
[Sun Sept/13]
Sunderland AFC 0-1 Tottenham Hotspur
Leicester City 3-2 Aston Villa
[Mon Sept/14]
West Ham United 2-0 Newcastle United
Matchday 6
[Sat Sept/19]
Chelsea FC 2-0 Arsenal FC
AFC Bournemouth 2-0 Sunderland AFC
Aston Villa 0-1 West Bromwich Albion
Newcastle United 1-2 Watford FC
Stoke City 2-2 Leicester City
Swansea City 0-0 Everton FC
Manchester City 1-2 West Ham United
[Sun Sept/20]
Tottenham Hotspur 1-0 Crystal Palace
Liverpool FC 1-1 Norwich City
Southampton FC 2-3 Manchester United
Matchday 7
[Sat Sept/26]
Tottenham Hotspur 4-1 Manchester City
Leicester City 2-5 Arsenal FC
Liverpool FC 3-2 Aston Villa
Manchester United 3-0 Sunderland AFC
Southampton FC 3-1 Swansea City
Stoke City 2-1 AFC Bournemouth
West Ham United 2-2 Norwich City
Newcastle United 2-2 Chelsea FC
[Sun Sept/27]
Watford FC 0-1 Crystal Palace
[Mon Sept/28]
West Bromwich Albion 2-3 Everton FC
Matchday 8
[Sat Oct/3]
AFC Bournemouth 1-1 Watford FC
Arsenal FC 3-0 Manchester United
Aston Villa 0-1 Stoke City
Chelsea FC 1-3 Southampton FC
Crystal Palace 2-0 West Bromwich Albion
Everton FC 1-1 Liverpool FC
Manchester City 6-1 Newcastle United
Norwich City 1-2 Leicester City
Sunderland AFC 2-2 West Ham United
Swansea City 2-2 Tottenham Hotspur
Matchday 9
[Sat Oct/17]
Chelsea FC 2-0 Aston Villa
Crystal Palace 1-3 West Ham United
Everton FC 0-3 Manchester United
Manchester City 5-1 AFC Bournemouth
Newcastle United 6-2 Norwich City
Southampton FC 2-2 Leicester City
Swansea City 0-1 Stoke City
Tottenham Hotspur 0-0 Liverpool FC
Watford FC 0-3 Arsenal FC
West Bromwich Albion 1-0 Sunderland AFC
Matchday 10
[Sat Oct/24]
AFC Bournemouth 1-5 Tottenham Hotspur
Arsenal FC 2-1 Everton FC
Aston Villa 1-2 Swansea City
Leicester City 1-0 Crystal Palace
Liverpool FC 1-1 Southampton FC
Manchester United 0-0 Manchester City
Norwich City 0-1 West Bromwich Albion
Stoke City 0-2 Watford FC
Sunderland AFC 3-0 Newcastle United
West Ham United 2-1 Chelsea FC
Matchday 11
[Sat Oct/31]
Chelsea FC 1-3 Liverpool FC
Crystal Palace 0-0 Manchester United
Everton FC 6-2 Sunderland AFC
Manchester City 2-1 Norwich City
Newcastle United 0-0 Stoke City
Southampton FC 2-0 AFC Bournemouth
Swansea City 0-3 Arsenal FC
Tottenham Hotspur 3-1 Aston Villa
Watford FC 2-0 West Ham United
West Bromwich Albion 2-3 Leicester City
Matchday 12
[Sat Nov/7]
AFC Bournemouth 0-1 Newcastle United
Arsenal FC 1-1 Tottenham Hotspur
Aston Villa 0-0 Manchester City
Leicester City 2-1 Watford FC
Liverpool FC 1-2 Crystal Palace
Manchester United 2-0 West Bromwich Albion
Norwich City 1-0 Swansea City
Stoke City 1-0 Chelsea FC
Sunderland AFC 0-1 Southampton FC
West Ham United 1-1 Everton FC
Matchday 13
[Sat Nov/21]
Chelsea FC 1-0 Norwich City
Crystal Palace 0-1 Sunderland AFC
Everton FC 4-0 Aston Villa
Manchester City 1-4 Liverpool FC
Newcastle United 0-3 Leicester City
Southampton FC 0-1 Stoke City
Swansea City 2-2 AFC Bournemouth
Tottenham Hotspur 4-1 West Ham United
Watford FC 1-2 Manchester United
West Bromwich Albion 2-1 Arsenal FC
Matchday 14
[Sat Nov/28]
AFC Bournemouth 3-3 Everton FC
Aston Villa 2-3 Watford FC
Crystal Palace 5-1 Newcastle United
Leicester City 1-1 Manchester United
Liverpool FC 1-0 Swansea City
Manchester City 3-1 Southampton FC
Norwich City 1-1 Arsenal FC
Sunderland AFC 2-0 Stoke City
Tottenham Hotspur 0-0 Chelsea FC
West Ham United 1-1 West Bromwich Albion
Matchday 15
[Sat Dec/5]
Arsenal FC 3-1 Sunderland AFC
Chelsea FC 0-1 AFC Bournemouth
Everton FC 1-1 Crystal Palace
Manchester United 0-0 West Ham United
Newcastle United 2-0 Liverpool FC
Southampton FC 1-1 Aston Villa
Stoke City 2-0 Manchester City
Swansea City 0-3 Leicester City
Watford FC 2-0 Norwich City
West Bromwich Albion 1-1 Tottenham Hotspur
Matchday 16
[Sat Dec/12]
AFC Bournemouth 2-1 Manchester United
Aston Villa 0-2 Arsenal FC
Crystal Palace 1-0 Southampton FC
Leicester City 2-1 Chelsea FC
Liverpool FC 2-2 West Bromwich Albion
Manchester City 2-1 Swansea City
Norwich City 1-1 Everton FC
Sunderland AFC 0-1 Watford FC
Tottenham Hotspur 1-2 Newcastle United
West Ham United 0-0 Stoke City
Matchday 17
[Sat Dec/19]
Arsenal FC 2-1 Manchester City
Chelsea FC 3-1 Sunderland AFC
Everton FC 2-3 Leicester City
Manchester United 1-2 Norwich City
Newcastle United 1-1 Aston Villa
Southampton FC 0-2 Tottenham Hotspur
Stoke City 1-2 Crystal Palace
Swansea City 0-0 West Ham United
Watford FC 3-0 Liverpool FC
West Bromwich Albion 1-2 AFC Bournemouth
Matchday 18
[Sat Dec/26]
AFC Bournemouth 0-0 Crystal Palace
Aston Villa 1-1 West Ham United
Chelsea FC 2-2 Watford FC
Liverpool FC 1-0 Leicester City
Manchester City 4-1 Sunderland AFC
Newcastle United 0-1 Everton FC
Southampton FC 4-0 Arsenal FC
Stoke City 2-0 Manchester United
Swansea City 1-0 West Bromwich Albion
Tottenham Hotspur 3-0 Norwich City
Matchday 19
[Mon Dec/28]
Arsenal FC 2-0 AFC Bournemouth
Crystal Palace 0-0 Swansea City
Everton FC 3-4 Stoke City
Leicester City 0-0 Manchester City
Manchester United 0-0 Chelsea FC
Norwich City 2-0 Aston Villa
Sunderland AFC 0-1 Liverpool FC
Watford FC 1-2 Tottenham Hotspur
West Bromwich Albion 1-0 Newcastle United
West Ham United 2-1 Southampton FC
###################################
# English Premier League 2015/16
Matchday 20
[Sat Jan/2]
Arsenal FC 1-0 Newcastle United
Leicester City 0-0 AFC Bournemouth
Manchester United 2-1 Swansea City
Norwich City 1-0 Southampton FC
Sunderland AFC 3-1 Aston Villa
Watford FC 1-2 Manchester City
West Bromwich Albion 2-1 Stoke City
West Ham United 2-0 Liverpool FC
[Sun Jan/3]
Crystal Palace 0-3 Chelsea FC
Everton FC 1-1 Tottenham Hotspur
Matchday 21
[Tue Jan/12]
AFC Bournemouth 1-3 West Ham United
Aston Villa 1-0 Crystal Palace
Liverpool FC 3-3 Arsenal FC
Swansea City 2-4 Sunderland AFC
[Wed Jan/13]
Chelsea FC 2-2 West Bromwich Albion
Manchester City 0-0 Everton FC
Newcastle United 3-3 Manchester United
Southampton FC 2-0 Watford FC
Stoke City 3-1 Norwich City
Tottenham Hotspur 0-1 Leicester City
Matchday 22
[Sat Jan/16]
AFC Bournemouth 3-0 Norwich City
Aston Villa 1-1 Leicester City
Chelsea FC 3-3 Everton FC
Manchester City 4-0 Crystal Palace
Newcastle United 2-1 West Ham United
Southampton FC 3-0 West Bromwich Albion
Tottenham Hotspur 4-1 Sunderland AFC
[Sun Jan/17]
Liverpool FC 0-1 Manchester United
Stoke City 0-0 Arsenal FC
[Mon Jan/18]
Swansea City 1-0 Watford FC
Matchday 23
[Sat Jan/23]
Crystal Palace 1-3 Tottenham Hotspur
Leicester City 3-0 Stoke City
Manchester United 0-1 Southampton FC
Norwich City 4-5 Liverpool FC
Sunderland AFC 1-1 AFC Bournemouth
Watford FC 2-1 Newcastle United
West Bromwich Albion 0-0 Aston Villa
West Ham United 2-2 Manchester City
[Sun Jan/24]
Arsenal FC 0-1 Chelsea FC
Everton FC 1-2 Swansea City
Matchday 24
[Tue Feb/2]
Arsenal FC 0-0 Southampton FC
Crystal Palace 1-2 AFC Bournemouth
Leicester City 2-0 Liverpool FC
Manchester United 3-0 Stoke City
Norwich City 0-3 Tottenham Hotspur
Sunderland AFC 0-1 Manchester City
Watford FC 0-0 Chelsea FC
West Bromwich Albion 1-1 Swansea City
West Ham United 2-0 Aston Villa
[Wed Feb/3]
Everton FC 3-0 Newcastle United
Matchday 25
[Sat Feb/6]
Aston Villa 2-0 Norwich City
Liverpool FC 2-2 Sunderland AFC
Manchester City 1-3 Leicester City
Newcastle United 1-0 West Bromwich Albion
Stoke City 0-3 Everton FC
Swansea City 1-1 Crystal Palace
Tottenham Hotspur 1-0 Watford FC
[Sun Feb/7]
AFC Bournemouth 0-2 Arsenal FC
Chelsea FC 1-1 Manchester United
[Mon Feb/8]
Southampton FC 1-0 West Ham United
Matchday 26
[Sat Feb/13]
AFC Bournemouth 1-3 Stoke City
Chelsea FC 5-1 Newcastle United
Crystal Palace 1-2 Watford FC
Everton FC 0-1 West Bromwich Albion
Norwich City 2-2 West Ham United
Sunderland AFC 2-1 Manchester United
Swansea City 0-1 Southampton FC
[Sun Feb/14]
Arsenal FC 2-1 Leicester City
Aston Villa 0-6 Liverpool FC
Manchester City 1-2 Tottenham Hotspur
Matchday 27
[Sat Feb/27]
Leicester City 1-0 Norwich City
Southampton FC 1-2 Chelsea FC
Stoke City 2-1 Aston Villa
Watford FC 0-0 AFC Bournemouth
West Bromwich Albion 3-2 Crystal Palace
West Ham United 1-0 Sunderland AFC
[Sun Feb/28]
Liverpool FC 4-0 Everton FC
Manchester United 3-2 Arsenal FC
Newcastle United 1-1 Manchester City
Tottenham Hotspur 2-1 Swansea City
Matchday 28
[Tue Mar/1]
AFC Bournemouth 2-0 Southampton FC
Aston Villa 1-3 Everton FC
Leicester City 2-2 West Bromwich Albion
Norwich City 1-2 Chelsea FC
Sunderland AFC 2-2 Crystal Palace
[Wed Mar/2]
Arsenal FC 1-2 Swansea City
Liverpool FC 3-0 Manchester City
Manchester United 1-0 Watford FC
Stoke City 1-0 Newcastle United
West Ham United 1-0 Tottenham Hotspur
Matchday 29
[Sat Mar/5]
Chelsea FC 1-1 Stoke City
Everton FC 2-3 West Ham United
Manchester City 4-0 Aston Villa
Newcastle United 1-3 AFC Bournemouth
Southampton FC 1-1 Sunderland AFC
Swansea City 1-0 Norwich City
Tottenham Hotspur 2-2 Arsenal FC
Watford FC 0-1 Leicester City
[Sun Mar/6]
Crystal Palace 1-2 Liverpool FC
West Bromwich Albion 1-0 Manchester United
Matchday 30
[Sat Mar/12]
AFC Bournemouth 3-2 Swansea City
Arsenal FC 2-0 West Bromwich Albion
Aston Villa 0-2 Tottenham Hotspur
Manchester United 2-0 Crystal Palace
Norwich City 0-0 Manchester City
Stoke City 1-2 Southampton FC
Sunderland AFC 3-0 Everton FC
West Ham United 3-1 Watford FC
[Sun Mar/13]
Liverpool FC 1-1 Chelsea FC
[Mon Mar/14]
Leicester City 1-0 Newcastle United
Matchday 31
[Sat Mar/19]
Chelsea FC 2-2 West Ham United
Crystal Palace 0-1 Leicester City
Everton FC 0-2 Arsenal FC
Southampton FC 3-2 Liverpool FC
Swansea City 1-0 Aston Villa
Tottenham Hotspur 3-0 AFC Bournemouth
Watford FC 1-2 Stoke City
West Bromwich Albion 0-1 Norwich City
[Sun Mar/20]
Manchester City 0-1 Manchester United
Newcastle United 1-1 Sunderland AFC
Matchday 32
[Sat Apr/2]
AFC Bournemouth 0-4 Manchester City
Arsenal FC 4-0 Watford FC
Aston Villa 0-4 Chelsea FC
Leicester City 1-0 Southampton FC
Liverpool FC 1-1 Tottenham Hotspur
Manchester United 1-0 Everton FC
Norwich City 3-2 Newcastle United
Stoke City 2-2 Swansea City
Sunderland AFC 0-0 West Bromwich Albion
West Ham United 2-2 Crystal Palace
Matchday 33
[Sat Apr/9]
Aston Villa 1-2 AFC Bournemouth
Crystal Palace 1-0 Norwich City
Liverpool FC 4-1 Stoke City
Manchester City 2-1 West Bromwich Albion
Southampton FC 3-1 Newcastle United
Sunderland AFC 0-2 Leicester City
Swansea City 1-0 Chelsea FC
Tottenham Hotspur 3-0 Manchester United
Watford FC 1-1 Everton FC
West Ham United 3-3 Arsenal FC
Matchday 34
[Sat Apr/16]
AFC Bournemouth 1-2 Liverpool FC
Arsenal FC 1-1 Crystal Palace
Chelsea FC 0-3 Manchester City
Everton FC 1-1 Southampton FC
Leicester City 2-2 West Ham United
Manchester United 1-0 Aston Villa
Newcastle United 3-0 Swansea City
Norwich City 0-3 Sunderland AFC
Stoke City 0-4 Tottenham Hotspur
West Bromwich Albion 0-1 Watford FC
Matchday 35
[Sat Apr/23]
AFC Bournemouth 1-4 Chelsea FC
Aston Villa 2-4 Southampton FC
Crystal Palace 0-0 Everton FC
Leicester City 4-0 Swansea City
Liverpool FC 2-2 Newcastle United
Manchester City 4-0 Stoke City
Norwich City 4-2 Watford FC
Sunderland AFC 0-0 Arsenal FC
Tottenham Hotspur 1-1 West Bromwich Albion
West Ham United 3-2 Manchester United
Matchday 36
[Sat Apr/30]
Arsenal FC 1-0 Norwich City
Chelsea FC 2-2 Tottenham Hotspur
Everton FC 2-1 AFC Bournemouth
Manchester United 1-1 Leicester City
Newcastle United 1-0 Crystal Palace
Southampton FC 4-2 Manchester City
Stoke City 1-1 Sunderland AFC
Swansea City 3-1 Liverpool FC
Watford FC 3-2 Aston Villa
West Bromwich Albion 0-3 West Ham United
Leicester erstmals englischer Fussballmeister!
Absteiger Premiere League: Aston Villa, Newcastle United, Norwich City.
Aufsteiger in Premiere League: Burnley, FC Middlesborough, 1 Platz noch offen.
Matchday 37
[Sat May/7]
AFC Bournemouth 1-1 West Bromwich Albion
Aston Villa 0-0 Newcastle United
Crystal Palace 2-1 Stoke City
Leicester City 3-1 Everton FC
Liverpool FC 2-0 Watford FC
Manchester City 2-2 Arsenal FC
Norwich City 0-1 Manchester United
Sunderland AFC 3-2 Chelsea FC
Tottenham Hotspur 1-2 Southampton FC
West Ham United 1-4 Swansea City
Matchday 38
[Sun May/15]
Arsenal FC 4-0 Aston Villa
Chelsea FC 1-1 Leicester City
Everton FC 3-0 Norwich City
Manchester United 3-1 AFC Bournemouth
Newcastle United 5-1 Tottenham Hotspur
Southampton FC 4-1 Crystal Palace
Stoke City 2-1 West Ham United
Swansea City 1-1 Manchester City
Watford FC 2-2 Sunderland AFC
West Bromwich Albion 1-1 Liverpool FC
Play-Off um 1 Premierleague-Platz:
13.5.16 Sheffield Wednesday 2-0 Brighton&Hove Albion
16.5.16 Brighton&Hove Albion 1-1 Sheffield Wednesday
14.5.16 Derby Country 0-3 Hull City
17.5.16 Hull City 0-2 Derby Country
28.5.16 Hull City 1-0 Sheffield Wednesday
3. Aufsteiger in Premierleague:
Hull City.
Premierleague 2015/16 beendet.
Premierleague 2016/17 beginnt am 13.08.2016.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment