Skip to content

Instantly share code, notes, and snippets.

@cdepillabout
Created March 16, 2018 07:32
Show Gist options
  • Save cdepillabout/858ccb081cf19b60bbfc8f17d91b27aa to your computer and use it in GitHub Desktop.
Save cdepillabout/858ccb081cf19b60bbfc8f17d91b27aa to your computer and use it in GitHub Desktop.
small example of hlist from extensible library
module Parser where
import Control.Applicative
-- this is from the extensible package
import Data.Extensible.HList
import Data.Text
-- the shape of the problem
--
-- we want a list of Parsers, all of different types.
-- we will apply each of them, one after another,
-- in the context of asking questions in some monadic
-- context.
data QParser a = QParser Text (Text -> Maybe a)
parse :: (Text -> Maybe a) -> Text -> Maybe a
parse = undefined
-- collectAll2 :: Monad m
-- => (Text -> m Text)
-- -> QParser a -> QParser b
-- -> m (Maybe (a,b))
-- collectAll2 ask (q1,p1) (q2,p2) =
-- (liftA2 (,))
-- <$> (parse p1 <$> ask q1)
-- <*> (parse p2 <$> ask q2)
-- collectAll3 :: Monad m
-- => (Text -> m Text)
-- -> QParser a -> QParser b -> QParser c
-- -> m (Maybe (a,b,c))
-- collectAll3 ask (q1,p1) (q2,p2) (q3,p3) =
-- liftA3 (,,)
-- <$> (parse p1 <$> ask q1)
-- <*> (parse p2 <$> ask q2)
-- <*> (parse p3 <$> ask q3)
-- liftA4 doesn't exist :(
-- collectAll4 :: Monad m
-- => (Text -> m Text)
-- -> QParser a -> QParser b -> QParser c -> QParser d
-- -> m (Maybe (a,b,c,d))
-- collectAll4 ask (q1,p1) (q2,p2) (q3,p3) (q4,p4) =
-- liftA4 (,,,)
-- <*> (parse p1 <$> ask q1)
-- <*> (parse p2 <$> ask q2)
-- <*> (parse p3 <$> ask q3)
-- <*> (parse p4 <$> ask q4)
collectAll
:: Monad m
=> (Text -> m Text)
-> HList QParser es
-> m (HList Maybe es)
collectAll ask = htraverse $ \(QParser text parser) -> fmap parser (ask text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment