on master help sign in
Add hoist and generally
commit02efa2ccefce4d196d32c7cfa33826a8d85cdbe3 authorHarendra Kumar <harendra.kumar@gmail.com> dateSat 9 Nov 2019, 03:05 (7 years ago) parent2b33253
1 file changed+23
src/Streamly/Internal/Data/Fold.hs+23
src/Streamly/Internal/Data/Fold.hs+23
@@ -26,6 +26,9 @@ module Streamly.Internal.Data.Fold
2626 -- * Fold Type
2727 Fold (..)
2828
29+ , hoist
30+ , generally
31+
2932 -- , tail
3033 -- , init
3134
@@ -180,6 +183,7 @@ module Streamly.Internal.Data.Fold
180183where
181184
182185import Control.Monad (void)
186+import Data.Functor.Identity (Identity(..))
183187import Data.Map.Strict (Map)
184188
185189import Prelude
@@ -244,6 +248,25 @@ mkFold = Fold
244248mkFoldId :: Monad m => (b -> a -> m b) -> m b -> Fold m a b
245249mkFoldId step initial = Fold step initial return
246250
251+------------------------------------------------------------------------------
252+-- hoist
253+------------------------------------------------------------------------------
254+
255+-- | Change the underlying monad of a fold
256+--
257+-- /Internal/
258+hoist :: (forall x. m x -> n x) -> Fold m a b -> Fold n a b
259+hoist f (Fold step initial extract) =
260+ Fold (\x a -> f $ step x a) (f initial) (f . extract)
261+
262+-- | Adapt a pure fold to any monad
263+--
264+-- > generally = hoist (return . runIdentity)
265+--
266+-- /Internal/
267+generally :: Monad m => Fold Identity a b -> Fold m a b
268+generally = hoist (return . runIdentity)
269+
247270------------------------------------------------------------------------------
248271-- Transformations on fold inputs
249272------------------------------------------------------------------------------