Skip to content

Instantly share code, notes, and snippets.

@fumieval
Forked from cdepillabout/extensible.hs
Last active March 17, 2018 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fumieval/c4b1b18beca142d8b06631e0cfeb8c9b to your computer and use it in GitHub Desktop.
Save fumieval/c4b1b18beca142d8b06631e0cfeb8c9b to your computer and use it in GitHub Desktop.
small example of hlist from extensible library
{-# LANGUAGE TypeOperators #-}
import Control.Applicative
import Data.Extensible
import Control.Monad.Trans.Maybe
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
{ question :: Text
, parse :: Text -> Maybe a
}
collectAll :: Monad m
=> (Text -> m Text)
-> QParser :* xs
-> m (Maybe (AllOf xs))
collectAll ask = runMaybeT
. htraverse (\(QParser q p) -> MaybeT $ fmap pure . p <$> ask q)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment