Skip to content

Instantly share code, notes, and snippets.

@etorreborre
Forked from fumieval/extensible.hs
Created March 17, 2018 20:48
Show Gist options
  • Save etorreborre/820e40d825c1d4976046aee124ee7948 to your computer and use it in GitHub Desktop.
Save etorreborre/820e40d825c1d4976046aee124ee7948 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