Servant.Types.SourceT

servant-0.20.2: A family of combinators for defining webservices APIs

Safe Haskell | None
Language | Haskell2010

Servant.Types.SourceT

Synopsis

- newtype SourceT (m :: Type -> Type) a = SourceT {
  - unSourceT :: forall b. (StepT m a -> m b) -> m b
  }
- mapStepT :: forall (m :: Type -> Type) a b. (StepT m a -> StepT m b) -> SourceT m a -> SourceT m b
- data StepT (m :: Type -> Type) a
  - = Stop
  - | Error String
  - | Skip (StepT m a)
  - | Yield a (StepT m a)
  - | Effect (m (StepT m a))
- fromStepT :: forall (m :: Type -> Type) a. StepT m a -> SourceT m a
- source :: forall f a (m :: Type -> Type). Foldable f => f a -> SourceT m a
- runSourceT :: forall (m :: Type -> Type) a. Monad m => SourceT m a -> ExceptT String m [a]
- runStepT :: forall (m :: Type -> Type) a. Monad m => StepT m a -> ExceptT String m [a]
- mapMaybe :: forall (m :: Type -> Type) a b. Functor m => (a -> Maybe b) -> SourceT m a -> SourceT m b
- mapMaybeStep :: forall (m :: Type -> Type) a b. Functor m => (a -> Maybe b) -> StepT m a -> StepT m b
- foreach :: Monad m => (String -> m ()) -> (a -> m ()) -> SourceT m a -> m ()
- foreachStep :: Monad m => (String -> m ()) -> (a -> m ()) -> StepT m a -> m ()
- fromAction :: Functor m => (a -> Bool) -> m a -> SourceT m a
- fromActionStep :: Functor m => (a -> Bool) -> m a -> StepT m a
- readFile :: FilePath -> SourceT IO ByteString
- transformWithAtto :: forall (m :: Type -> Type) a. Monad m => Parser a -> SourceT m ByteString -> SourceT m a
- transformStepWithAtto :: forall a (m :: Type -> Type). Monad m => Parser a -> StepT m ByteString -> StepT m a

# Documentation

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

newtype SourceT (m :: Type -> Type) a #

This is CPSised ListT.

Since: 0.15

Constructors

SourceT |
Fields unSourceT :: forall b. (StepT m a -> m b) -> m b

#### Instances

Instances details

MFunctor SourceT # |
    >>> 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 :: Monad m => (forall a. m a -> n a) -> SourceT m b -> SourceT n b #
MonadIO m => FromSourceIO a (SourceT m a) # |
Instance details Defined in Servant.API.Stream Methods fromSourceIO :: SourceIO a -> IO (SourceT m a) #
SourceToSourceIO m => ToSourceIO chunk (SourceT m chunk) # | Relax to use auxiliary class, have m
Instance details Defined in Servant.API.Stream Methods toSourceIO :: SourceT m chunk -> SourceIO chunk #
(Applicative m, Show1 m) => Show1 (SourceT m) # |
Instance details Defined in Servant.Types.SourceT Methods liftShowsPrec :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> SourceT m a -> ShowS # liftShowList :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> [SourceT m a] -> ShowS #
Functor m => Functor (SourceT m) # |
Instance details Defined in Servant.Types.SourceT Methods fmap :: (a -> b) -> SourceT m a -> SourceT m b # (<$) :: a -> SourceT m b -> SourceT m a #
Identity ~ m => Foldable (SourceT m) # |
    >>> toList (source [1::Int .. 10])
    [1,2,3,4,5,6,7,8,9,10]
Instance details Defined in Servant.Types.SourceT Methods fold :: Monoid m0 => SourceT m m0 -> m0 # foldMap :: Monoid m0 => (a -> m0) -> SourceT m a -> m0 # foldMap' :: Monoid m0 => (a -> m0) -> SourceT m a -> m0 # foldr :: (a -> b -> b) -> b -> SourceT m a -> b # foldr' :: (a -> b -> b) -> b -> SourceT m a -> b # foldl :: (b -> a -> b) -> b -> SourceT m a -> b # foldl' :: (b -> a -> b) -> b -> SourceT m a -> b # foldr1 :: (a -> a -> a) -> SourceT m a -> a # foldl1 :: (a -> a -> a) -> SourceT m a -> a # toList :: SourceT m a -> [a] # null :: SourceT m a -> Bool # length :: SourceT m a -> Int # elem :: Eq a => a -> SourceT m a -> Bool # maximum :: Ord a => SourceT m a -> a # minimum :: Ord a => SourceT m a -> a # sum :: Num a => SourceT m a -> a # product :: Num a => SourceT m a -> a #
(Arbitrary a, Monad m) => Arbitrary (SourceT m a) # | Doesn't generate `Error` constructors. `SourceT` doesn't shrink.
Instance details Defined in Servant.Types.SourceT Methods arbitrary :: Gen (SourceT m a) # shrink :: SourceT m a -> [SourceT m a] #
Functor m => Monoid (SourceT m a) # |
    >>> mempty :: SourceT Maybe Int
    fromStepT (Effect (Just Stop))
Instance details Defined in Servant.Types.SourceT Methods mempty :: SourceT m a # mappend :: SourceT m a -> SourceT m a -> SourceT m a # mconcat :: [SourceT m a] -> SourceT m a #
Functor m => Semigroup (SourceT m a) # |
    >>> source "xy" <> source "z" :: SourceT Identity Char
    fromStepT (Effect (Identity (Yield 'x' (Yield 'y' (Yield 'z' Stop)))))
Instance details Defined in Servant.Types.SourceT Methods (<>) :: SourceT m a -> SourceT m a -> SourceT m a # sconcat :: NonEmpty (SourceT m a) -> SourceT m a # stimes :: Integral b => b -> SourceT m a -> SourceT m a #
(Applicative m, Show1 m, Show a) => Show (SourceT m a) # |
Instance details Defined in Servant.Types.SourceT Methods showsPrec :: Int -> SourceT m a -> ShowS # show :: SourceT m a -> String # showList :: [SourceT m a] -> ShowS #

mapStepT :: forall (m :: Type -> Type) a b. (StepT m a -> StepT m b) -> SourceT m a -> SourceT m b #

data StepT (m :: Type -> Type) a #

`ListT` with additional constructors.

Since: 0.15

Constructors

Stop |
Error String |
Skip (StepT m a) |
Yield a (StepT m a) |
Effect (m (StepT m a)) |

#### Instances

Instances details

MFunctor StepT # |
Instance details Defined in Servant.Types.SourceT Methods hoist :: Monad m => (forall a. m a -> n a) -> StepT m b -> StepT n b #
(Applicative m, Show1 m) => Show1 (StepT m) # |
Instance details Defined in Servant.Types.SourceT Methods liftShowsPrec :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> StepT m a -> ShowS # liftShowList :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> [StepT m a] -> ShowS #
Functor m => Functor (StepT m) # |
Instance details Defined in Servant.Types.SourceT Methods fmap :: (a -> b) -> StepT m a -> StepT m b # (<$) :: a -> StepT m b -> StepT m a #
Identity ~ m => Foldable (StepT m) # |
Instance details Defined in Servant.Types.SourceT Methods fold :: Monoid m0 => StepT m m0 -> m0 # foldMap :: Monoid m0 => (a -> m0) -> StepT m a -> m0 # foldMap' :: Monoid m0 => (a -> m0) -> StepT m a -> m0 # foldr :: (a -> b -> b) -> b -> StepT m a -> b # foldr' :: (a -> b -> b) -> b -> StepT m a -> b # foldl :: (b -> a -> b) -> b -> StepT m a -> b # foldl' :: (b -> a -> b) -> b -> StepT m a -> b # foldr1 :: (a -> a -> a) -> StepT m a -> a # foldl1 :: (a -> a -> a) -> StepT m a -> a # toList :: StepT m a -> [a] # null :: StepT m a -> Bool # length :: StepT m a -> Int # elem :: Eq a => a -> StepT m a -> Bool # maximum :: Ord a => StepT m a -> a # minimum :: Ord a => StepT m a -> a # sum :: Num a => StepT m a -> a # product :: Num a => StepT m a -> a #
(Arbitrary a, Monad m) => Arbitrary (StepT m a) # | Doesn't generate `Error` constructors.
Instance details Defined in Servant.Types.SourceT Methods arbitrary :: Gen (StepT m a) # shrink :: StepT m a -> [StepT m a] #
Functor m => Monoid (StepT m a) # |
    >>> mempty :: StepT [] Int
    Stop
    >>> mempty :: StepT Identity Int
    Stop
Instance details Defined in Servant.Types.SourceT Methods mempty :: StepT m a # mappend :: StepT m a -> StepT m a -> StepT m a # mconcat :: [StepT m a] -> StepT m a #
Functor m => Semigroup (StepT m a) # |
Instance details Defined in Servant.Types.SourceT Methods (<>) :: StepT m a -> StepT m a -> StepT m a # sconcat :: NonEmpty (StepT m a) -> StepT m a # stimes :: Integral b => b -> StepT m a -> StepT m a #
(Applicative m, Show1 m, Show a) => Show (StepT m a) # |
Instance details Defined in Servant.Types.SourceT Methods showsPrec :: Int -> StepT m a -> ShowS # show :: StepT m a -> String # showList :: [StepT m a] -> ShowS #

fromStepT :: forall (m :: Type -> Type) a. StepT m a -> SourceT m a #

Create `SourceT` from `Step`.

Note: often enough you want to use `SourceT` directly.

source :: forall f a (m :: Type -> Type). Foldable f => f a -> SourceT m a #

Create pure `SourceT`.

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

runSourceT :: forall (m :: Type -> Type) a. Monad m => SourceT m a -> ExceptT String m [a] #

Get the answers.

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

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

runStepT :: forall (m :: Type -> Type) a. Monad m => StepT m a -> ExceptT String m [a] #

mapMaybe :: forall (m :: Type -> Type) a b. Functor m => (a -> Maybe b) -> SourceT m a -> SourceT m b #

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`.

mapMaybeStep :: forall (m :: Type -> Type) a b. Functor m => (a -> Maybe b) -> StepT m a -> StepT m b #

foreach #

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" :: String)
    'a'
    'b'
    'c'

foreachStep #

Arguments

:: Monad m |
=> (String -> m ()) | error handler
-> (a -> m ()) |
-> StepT m a |
-> m () |

See `foreach`.

fromAction :: Functor m => (a -> Bool) -> m a -> SourceT m a #

fromActionStep :: Functor m => (a -> Bool) -> m a -> StepT m a #

readFile :: FilePath -> SourceT IO ByteString #

Read file.

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

transformWithAtto :: forall (m :: Type -> Type) a. Monad m => Parser a -> SourceT m ByteString -> SourceT m a #

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 $ [fromString "1 2 3"])
    Right ["1","2","3"]

    >>> runExcept $ runSourceT $ transformWithAtto parser (source $ map fromString ["1", "2", "3"])
    Right ["123"]

    >>> runExcept $ runSourceT $ transformWithAtto parser (source $ map fromString ["1", "2 3", "4"])
    Right ["12","34"]

    >>> runExcept $ runSourceT $ transformWithAtto parser (source [fromString "foobar"])
    Left "Failed reading: takeWhile1"

transformStepWithAtto :: forall a (m :: Type -> Type). Monad m => Parser a -> StepT m ByteString -> StepT m a #