2{-# LANGUAGE ExistentialQuantification #-}
5-- Module : Streamly.Fold.Types
6-- Copyright : (c) 2019 Composewell Technologies
7-- (c) 2013 Gabriel Gonzalez
9-- Maintainer : harendra.kumar@gmail.com
10-- Stability : experimental
13module Streamly.Fold.Types
18import Control.Applicative (liftA2)
19import Data.Semigroup (Semigroup(..))
20import Streamly.Strict (Tuple'(..))
22------------------------------------------------------------------------------
24------------------------------------------------------------------------------
26-- | Represents a left fold over an input stream of values of type @a@ to a
27-- single value of type @b@ in 'Monad' @m@.
29-- The fold uses an intermediate type @x@ as accumulator. The fold accumulator
30-- is initialized by calling the @init@ function and is then driven by calling
31-- the step function repeatedly. When the fold is done the @extract@ function
32-- is used to map the intermediate type @x@ to the final type @b@. This allows
33-- the state of the fold to be embedded in an arbitrary type @x@.
36 -- | @Fold @ @ step @ @ initial @ @ extract@
37 forall x. Fold (x -> a -> m x) (m x) (x -> m b)
39-- | Maps a function on the output of the fold (the type @b@).
40instance Applicative m => Functor (Fold m a) where
42 fmap f (Fold step start done) = Fold step start done'
44 done' x = fmap f $! done x
49-- | The input (type @a@) of the composed fold is distributed to both the
50-- constituent folds. The outputs of the constituent folds (type @b@) are
51-- applied to a function.
52instance Applicative m => Applicative (Fold m a) where
54 pure b = Fold (\() _ -> pure ()) (pure ()) (\() -> pure b)
57 (Fold stepL beginL doneL) <*> (Fold stepR beginR doneR) =
58 let step (Tuple' xL xR) a = Tuple' <$> stepL xL a <*> stepR xR a
59 begin = Tuple' <$> beginL <*> beginR
60 done (Tuple' xL xR) = doneL xL <*> doneR xR
61 in Fold step begin done
69-- | Combines the outputs of the folds (the type @b@) using their 'Semigroup'
71instance (Semigroup b, Monad m) => Semigroup (Fold m a b) where
75-- | Combines the outputs of the folds (the type @b@) using their 'Monoid'
77instance (Semigroup b, Monoid b, Monad m) => Monoid (Fold m a b) where
81 {-# INLINE mappend #-}
84-- | Combines the fold outputs (type @b@) using their 'Num' instances.
85instance (Monad m, Num b) => Num (Fold m a b) where
86 {-# INLINE fromInteger #-}
87 fromInteger = pure . fromInteger
107-- | Combines the fold outputs (type @b@) using their 'Fractional' instances.
108instance (Monad m, Fractional b) => Fractional (Fold m a b) where
109 {-# INLINE fromRational #-}
110 fromRational = pure . fromRational
118-- | Combines the fold outputs using their 'Floating' instances.
119instance (Monad m, Floating b) => Floating (Fold m a b) where
171 {-# INLINE logBase #-}
172 logBase = liftA2 logBase
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