GoogleCodeJam-0.0.3: A monad for flexible parsing of Google Code Jam input files with automatic parallelization.

Safe HaskellNone
LanguageHaskell2010

GCJ

Contents

Description

This defines a Google Code Jam monad with a handful of combinators for simple parsing of input files.

The test cases are solved in parallel.

Although the parsing of the input cannot happen in parallel (lines are consumed one by one, and input of the nexta example depends on how much the current has consumed), the actual work can be automatically parallelized, if the number of lines to consume does not depend on the result of the main workload of each case.

This is demonstrated by the following example:

parallel test
mainP = mainId $ do
       r <- return . length . show . product . enumFromTo (1::Integer) . read =<< cLine
       if not parallel && r == 1 then return =<< cLine else return $ show r  
       where parallel = False

input file:

2
200000
200000
200000
200000

Synopsis

Documentation

newtype GCJ m a Source #

data type for Google Code Jam Solution Program

It has to be a Monad (e.g. to implement cBlock).

Next action has to be able to depend on state.

Constructors

GCJ 

Fields

Instances

MonadTrans GCJ Source # 

Methods

lift :: Monad m => m a -> GCJ m a #

Monad m => Monad (GCJ m) Source # 

Methods

(>>=) :: GCJ m a -> (a -> GCJ m b) -> GCJ m b #

(>>) :: GCJ m a -> GCJ m b -> GCJ m b #

return :: a -> GCJ m a #

fail :: String -> GCJ m a #

Functor m => Functor (GCJ m) Source # 

Methods

fmap :: (a -> b) -> GCJ m a -> GCJ m b #

(<$) :: a -> GCJ m b -> GCJ m a #

Monad m => Applicative (GCJ m) Source # 

Methods

pure :: a -> GCJ m a #

(<*>) :: GCJ m (a -> b) -> GCJ m a -> GCJ m b #

(*>) :: GCJ m a -> GCJ m b -> GCJ m b #

(<*) :: GCJ m a -> GCJ m b -> GCJ m a #

MonadIO m => MonadIO (GCJ m) Source # 

Methods

liftIO :: IO a -> GCJ m a #

Combinators

cLine :: Monad m => GCJ m String Source #

consumes and returns one line

cLines :: Monad m => Int -> GCJ m [String] Source #

consumes and returns n lines

cBlock :: Monad m => Int -> ([String] -> a) -> GCJ m (Int, a) Source #

consumes, transforms and returns next block:

a block consists of first line: n, then (n-m) lines

Runners

type Runner m = GCJ m String -> [String] -> IO () Source #

mainId :: Runner Identity Source #

produce main from pure function

mainIO :: Runner IO Source #

produce main from with transformed IO monad

mainM :: Monad m => (forall a. m a -> IO a) -> Runner m Source #

produce main from Monad

reads testInput or the file given on the command line

writes to standard out and to the given file with '.out' appended