2-- Module : Streamly.Internal.Data.Fold.Step
3-- Copyright : (c) 2019 Composewell Technologies
5-- Maintainer : streamly@composewell.com
6-- Stability : experimental
9module Streamly.Internal.Data.Fold.Step
19import Data.Bifunctor (Bifunctor(..))
20import Fusion.Plugin.Types (Fuse(..))
22------------------------------------------------------------------------------
24------------------------------------------------------------------------------
26-- The Step functor around b allows expressing early termination like a right
27-- fold. Traditional list right folds use function composition and laziness to
28-- terminate early whereas we use data constructors. It allows stream fusion in
29-- contrast to the foldr/build fusion when composing with functions.
31-- | Represents the result of the @step@ of a 'Fold'. 'Partial' returns an
32-- intermediate state of the fold, the fold step can be called again with the
33-- state or the driver can use @extract@ on the state to get the result out.
34-- 'Done' returns the final result and the fold cannot be driven further.
38{-# ANN type Step Fuse #-}
43-- | 'first' maps over 'Partial' and 'second' maps over 'Done'.
45instance Bifunctor Step where
47 bimap f _ (Partial a) = Partial (f a)
48 bimap _ g (Done b) = Done (g b)
51 first f (Partial a) = Partial (f a)
52 first _ (Done x) = Done x
55 second _ (Partial x) = Partial x
56 second f (Done a) = Done (f a)
58-- | 'fmap' maps over 'Done'.
64instance Functor (Step s) where
68-- | Map a monadic function over the result @b@ in @Step s b@.
71{-# INLINE mapMStep #-}
72mapMStep :: Applicative m => (a -> m b) -> Step s a -> m (Step s b)
75 Partial s -> pure $ Partial s
76 Done b -> Done <$> f b
78-- | If 'Partial' then map the state, if 'Done' then call the next step.
79{-# INLINE chainStepM #-}
80chainStepM :: Applicative m =>
81 (s1 -> m s2) -> (a -> m (Step s2 b)) -> Step s1 a -> m (Step s2 b)
82chainStepM f _ (Partial s) = Partial <$> f s
83chainStepM _ g (Done b) = g b
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list