-
-
Save fumieval/c4b1b18beca142d8b06631e0cfeb8c9b to your computer and use it in GitHub Desktop.
small example of hlist from extensible library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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