on master help sign in
Move hoist/generally to lower level Fold module

So that we can use these in lower level array modules.

commit9ccaae197bb78e5f7fa5c3241cdbf34b4b21a7ba authorHarendra Kumar <harendra@composewell.com> dateTue 13 Dec 2022, 03:39 (4 years ago) parent1349b8c
2 files changed+24−20
core/src/Streamly/Internal/Data/Fold.hs−20 core/src/Streamly/Internal/Data/Fold/Type.hs+24
core/src/Streamly/Internal/Data/Fold.hs−20
@@ -304,7 +304,6 @@ import Control.Monad.IO.Class (MonadIO(..))
304304import Data.Bifunctor (first)
305305import Data.Bits (shiftL, shiftR, (.|.), (.&.))
306306import Data.Either (isLeft, isRight, fromLeft, fromRight)
307−import Data.Functor.Identity (Identity(..))
308307import Data.Int (Int64)
309308import Data.Word (Word32)
310309import Foreign.Storable (peek, sizeOf)
@@ -408,25 +407,6 @@ driveBreak strm fl = fmap f $ K.foldBreak fl (Stream.toStreamK strm)
408407augment :: Monad m => Stream m a -> Fold m a b -> m (Fold m a b)
409408augment stream = drive stream . duplicate
410409
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−
430410------------------------------------------------------------------------------
431411-- Transformations on fold inputs
432412------------------------------------------------------------------------------
core/src/Streamly/Internal/Data/Fold/Type.hs+24
@@ -410,6 +410,10 @@ module Streamly.Internal.Data.Fold.Type
410410 , close
411411 , isClosed
412412
413+ -- * Lifting inner monad
414+ , hoist
415+ , generally
416+
413417 -- * Deprecated
414418 , foldr
415419 , serialWith
@@ -422,6 +426,7 @@ import Control.Applicative (liftA2)
422426import Control.Monad ((>=>))
423427import Data.Bifunctor (Bifunctor(..))
424428import Data.Either (fromLeft, fromRight, isLeft, isRight)
429+import Data.Functor.Identity (Identity(..))
425430import Fusion.Plugin.Types (Fuse(..))
426431import Streamly.Internal.Data.Fold.Step (Step(..), mapMStep, chainStepM)
427432import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
@@ -1817,3 +1822,22 @@ refoldMany1 (Refold sstep sinject sextract) (Fold cstep cinitial cextract) =
18171822refold :: Monad m => Refold m b a c -> Fold m a b -> Fold m a c
18181823refold (Refold step inject extract) f =
18191824 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)