servant-0.19.1: A family of combinators for defining webservices APIs
Safe Haskell Safe-Inferred
Language Haskell2010

Servant.Types.SourceT

Synopsis

Documentation

>>> :set -XOverloadedStrings
>>> import Control.Monad.Except (runExcept)
>>> import Data.Foldable (toList)
>>> import qualified Data.Attoparsec.ByteString.Char8 as A8

newtype SourceT m a Source #

This is CPSised ListT.

Since: 0.15

Constructors

SourceT

Fields

Instances

Instances details
MonadIO m => FromSourceIO a ( SourceT m a) Source #
Instance details

Defined in Servant.API.Stream

SourceToSourceIO m => ToSourceIO chunk ( SourceT m chunk) Source #

Relax to use auxiliary class, have m

Instance details

Defined in Servant.API.Stream

Functor m => Functor ( SourceT m) Source #
Instance details

Defined in Servant.Types.SourceT

Identity ~ m => Foldable ( SourceT m) Source #
>>> toList (source [1..10])
[1,2,3,4,5,6,7,8,9,10]
Instance details

Defined in Servant.Types.SourceT

( Applicative m, Show1 m) => Show1 ( SourceT m) Source #
Instance details

Defined in Servant.Types.SourceT

MFunctor SourceT Source #
>>> hoist (Just . runIdentity) (source [1..3]) :: SourceT Maybe Int
fromStepT (Effect (Just (Yield 1 (Yield 2 (Yield 3 Stop)))))
Instance details

Defined in Servant.Types.SourceT

Methods

hoist :: forall m n (b :: k). Monad m => ( forall a. m a -> n a) -> SourceT m b -> SourceT n b Source #

( Applicative m, Show1 m, Show a) => Show ( SourceT m a) Source #
Instance details

Defined in Servant.Types.SourceT

Functor m => Semigroup ( SourceT m a) Source #
>>> source "xy" <> source "z" :: SourceT Identity Char
fromStepT (Effect (Identity (Yield 'x' (Yield 'y' (Yield 'z' Stop)))))
Instance details

Defined in Servant.Types.SourceT

Functor m => Monoid ( SourceT m a) Source #
>>> mempty :: SourceT Maybe Int
fromStepT (Effect (Just Stop))
Instance details

Defined in Servant.Types.SourceT

( Arbitrary a, Monad m) => Arbitrary ( SourceT m a) Source #

Doesn't generate Error constructors. SourceT doesn't shrink.

Instance details

Defined in Servant.Types.SourceT

data StepT m a Source #

ListT with additional constructors.

Since: 0.15

Instances

Instances details
MonadTrans StepT Source #
>>> lift [1,2,3] :: StepT [] Int
Effect [Yield 1 Stop,Yield 2 Stop,Yield 3 Stop]
Instance details

Defined in Servant.Types.SourceT

Methods

lift :: Monad m => m a -> StepT m a Source #

Functor m => Functor ( StepT m) Source #
Instance details

Defined in Servant.Types.SourceT

Methods

fmap :: (a -> b) -> StepT m a -> StepT m b Source #

(<$) :: a -> StepT m b -> StepT m a Source #

Identity ~ m => Foldable ( StepT m) Source #
Instance details

Defined in Servant.Types.SourceT

( Applicative m, Show1 m) => Show1 ( StepT m) Source #
Instance details

Defined in Servant.Types.SourceT

MFunctor StepT Source #
Instance details

Defined in Servant.Types.SourceT

Methods

hoist :: forall m n (b :: k). Monad m => ( forall a. m a -> n a) -> StepT m b -> StepT n b Source #

( Applicative m, Show1 m, Show a) => Show ( StepT m a) Source #
Instance details

Defined in Servant.Types.SourceT

Functor m => Semigroup ( StepT m a) Source #
Instance details

Defined in Servant.Types.SourceT

Functor m => Monoid ( StepT m a) Source #
>>> mempty :: StepT [] Int
Stop
>>> mempty :: StepT Identity Int
Stop
Instance details

Defined in Servant.Types.SourceT

( Arbitrary a, Monad m) => Arbitrary ( StepT m a) Source #

Doesn't generate Error constructors.

Instance details

Defined in Servant.Types.SourceT

fromStepT :: StepT m a -> SourceT m a Source #

Create SourceT from Step .

Note: often enough you want to use SourceT directly.

source :: [a] -> SourceT m a Source #

Create pure SourceT .

>>> source "foo" :: SourceT Identity Char
fromStepT (Effect (Identity (Yield 'f' (Yield 'o' (Yield 'o' Stop)))))

runSourceT :: Monad m => SourceT m a -> ExceptT String m [a] Source #

Get the answers.

>>> runSourceT (source "foo" :: SourceT Identity Char)
ExceptT (Identity (Right "foo"))
>>> runSourceT (source "foo" :: SourceT [] Char)
ExceptT [Right "foo"]

mapMaybe :: Functor m => (a -> Maybe b) -> SourceT m a -> SourceT m b Source #

Filter values.

>>> toList $ mapMaybe (\x -> if odd x then Just x else Nothing) (source [0..10]) :: [Int]
[1,3,5,7,9]
>>> mapMaybe (\x -> if odd x then Just x else Nothing) (source [0..2]) :: SourceT Identity Int
fromStepT (Effect (Identity (Skip (Yield 1 (Skip Stop)))))

Illustrates why we need Skip .

foreach Source #

Arguments

:: Monad m
=> ( String -> m ())

error handler

-> (a -> m ())
-> SourceT m a
-> m ()

Run action for each value in the SourceT .

>>> foreach fail print (source "abc")
'a'
'b'
'c'

foreachStep Source #

Arguments

:: Monad m
=> ( String -> m ())

error handler

-> (a -> m ())
-> StepT m a
-> m ()

See foreach .

readFile :: FilePath -> SourceT IO ByteString Source #

Read file.

>>> foreach fail BS.putStr (readFile "servant.cabal")
cabal-version:       2.2
name:                servant
...

transformWithAtto :: Monad m => Parser a -> SourceT m ByteString -> SourceT m a Source #

Transform using attoparsec parser.

Note: parser should not accept empty input!

>>> let parser = A.skipWhile A8.isSpace_w8 >> A.takeWhile1 A8.isDigit_w8
>>> runExcept $ runSourceT $ transformWithAtto parser (source ["1 2 3"])
Right ["1","2","3"]
>>> runExcept $ runSourceT $ transformWithAtto parser (source ["1", "2", "3"])
Right ["123"]
>>> runExcept $ runSourceT $ transformWithAtto parser (source ["1", "2 3", "4"])
Right ["12","34"]
>>> runExcept $ runSourceT $ transformWithAtto parser (source ["foobar"])
Left "Failed reading: takeWhile1"