on master help sign in
Move hoist/generally to lower level Fold module
9ccaae1Harendra Kumar
core/src/Streamly/Internal/Data/Fold.hs−20
@@ -304,7 +304,6 @@ import Control.Monad.IO.Class (MonadIO(..))
304import Data.Bifunctor (first)
304import Data.Bifunctor (first)
305import Data.Bits (shiftL, shiftR, (.|.), (.&.))
305import Data.Bits (shiftL, shiftR, (.|.), (.&.))
306import Data.Either (isLeft, isRight, fromLeft, fromRight)
306import Data.Either (isLeft, isRight, fromLeft, fromRight)
307−import Data.Functor.Identity (Identity(..))
308import Data.Int (Int64)
307import Data.Int (Int64)
309import Data.Word (Word32)
308import Data.Word (Word32)
310import Foreign.Storable (peek, sizeOf)
309import Foreign.Storable (peek, sizeOf)
@@ -408,25 +407,6 @@ driveBreak strm fl = fmap f $ K.foldBreak fl (Stream.toStreamK strm)
408augment :: Monad m => Stream m a -> Fold m a b -> m (Fold m a b)
407augment :: Monad m => Stream m a -> Fold m a b -> m (Fold m a b)
409augment stream = drive stream . duplicate
408augment stream = drive stream . duplicate
410
409
411−------------------------------------------------------------------------------
412−-- hoist
413−------------------------------------------------------------------------------
414−
415−-- | Change the underlying monad of a fold
416−--
417−-- /Pre-release/
418−hoist :: (forall x. m x -> n x) -> Fold m a b -> Fold n a b
419−hoist f (Fold step initial extract) =
420− Fold (\x a -> f $ step x a) (f initial) (f . extract)
421−
422−-- | Adapt a pure fold to any monad
423−--
424−-- >>> generally = Fold.hoist (return . runIdentity)
425−--
426−-- /Pre-release/
427−generally :: Monad m => Fold Identity a b -> Fold m a b
428−generally = hoist (return . runIdentity)
429−
430------------------------------------------------------------------------------
410------------------------------------------------------------------------------
431-- Transformations on fold inputs
411-- Transformations on fold inputs
432------------------------------------------------------------------------------
412------------------------------------------------------------------------------
core/src/Streamly/Internal/Data/Fold/Type.hs+24
@@ -410,6 +410,10 @@ module Streamly.Internal.Data.Fold.Type
410 , close
410 , close
411 , isClosed
411 , isClosed
412
412
413+ -- * Lifting inner monad
414+ , hoist
415+ , generally
416+
413 -- * Deprecated
417 -- * Deprecated
414 , foldr
418 , foldr
415 , serialWith
419 , serialWith
@@ -422,6 +426,7 @@ import Control.Applicative (liftA2)
422import Control.Monad ((>=>))
426import Control.Monad ((>=>))
423import Data.Bifunctor (Bifunctor(..))
427import Data.Bifunctor (Bifunctor(..))
424import Data.Either (fromLeft, fromRight, isLeft, isRight)
428import Data.Either (fromLeft, fromRight, isLeft, isRight)
429+import Data.Functor.Identity (Identity(..))
425import Fusion.Plugin.Types (Fuse(..))
430import Fusion.Plugin.Types (Fuse(..))
426import Streamly.Internal.Data.Fold.Step (Step(..), mapMStep, chainStepM)
431import Streamly.Internal.Data.Fold.Step (Step(..), mapMStep, chainStepM)
427import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
432import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
@@ -1817,3 +1822,22 @@ refoldMany1 (Refold sstep sinject sextract) (Fold cstep cinitial cextract) =
1817refold :: Monad m => Refold m b a c -> Fold m a b -> Fold m a c
1822refold :: Monad m => Refold m b a c -> Fold m a b -> Fold m a c
1818refold (Refold step inject extract) f =
1823refold (Refold step inject extract) f =
1819 Fold step (extractM f >>= inject) extract
1824 Fold step (extractM f >>= inject) extract
1825+
1826+------------------------------------------------------------------------------
1827+-- hoist
1828+------------------------------------------------------------------------------
1829+
1830+-- | Change the underlying monad of a fold
1831+--
1832+-- /Pre-release/
1833+hoist :: (forall x. m x -> n x) -> Fold m a b -> Fold n a b
1834+hoist f (Fold step initial extract) =
1835+ Fold (\x a -> f $ step x a) (f initial) (f . extract)
1836+
1837+-- | Adapt a pure fold to any monad
1838+--
1839+-- >>> generally = Fold.hoist (return . runIdentity)
1840+--
1841+-- /Pre-release/
1842+generally :: Monad m => Fold Identity a b -> Fold m a b
1843+generally = hoist (return . runIdentity)