on master help sign in
Remove dependency of Array/Types on StreamK
8af89ddHarendra Kumar
src/Streamly/Internal/Data/Array/Storable/Foreign/Mut/Types.hs−1
@@ -123,7 +123,6 @@ import qualified Data.Foldable as F
123import qualified GHC.Exts as Exts
123import qualified GHC.Exts as Exts
124import qualified Streamly.Internal.Data.Fold.Types as FL
124import qualified Streamly.Internal.Data.Fold.Types as FL
125import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
125import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
126−import qualified Streamly.Internal.Data.Stream.StreamK as K (foldl')
127import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
126import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
128import qualified Streamly.Internal.Foreign.Malloc as Malloc
127import qualified Streamly.Internal.Foreign.Malloc as Malloc
129
128
src/Streamly/Internal/Data/Stream/StreamK.hs−38
@@ -383,44 +383,6 @@ foldr1 step m = do
383 yieldk a r = fmap (step p) (go a r)
383 yieldk a r = fmap (step p) (go a r)
384 in foldStream defState yieldk single stp m1
384 in foldStream defState yieldk single stp m1
385
385
386−-- | Strict left fold with an extraction function. Like the standard strict
387−-- left fold, but applies a user supplied extraction function (the third
388−-- argument) to the folded value at the end. This is designed to work with the
389−-- @foldl@ library. The suffix @x@ is a mnemonic for extraction.
390−--
391−-- Note that the accumulator is always evaluated including the initial value.
392−{-# INLINE foldlx' #-}
393−foldlx' :: forall t m a b x. (IsStream t, Monad m)
394− => (x -> a -> x) -> x -> (x -> b) -> t m a -> m b
395−foldlx' step begin done m = get $ go m begin
396− where
397− {-# NOINLINE get #-}
398− get :: t m x -> m b
399− get m1 =
400− -- XXX we are not strictly evaluating the accumulator here. Is this
401− -- okay?
402− let single = return . done
403− -- XXX this is foldSingleton. why foldStreamShared?
404− in foldStreamShared undefined undefined single undefined m1
405−
406− -- Note, this can be implemented by making a recursive call to "go",
407− -- however that is more expensive because of unnecessary recursion
408− -- that cannot be tail call optimized. Unfolding recursion explicitly via
409− -- continuations is much more efficient.
410− go :: t m a -> x -> t m x
411− go m1 !acc = mkStream $ \_ yld sng _ ->
412− let stop = sng acc
413− single a = sng $ step acc a
414− -- XXX this is foldNonEmptyStream
415− yieldk a r = foldStream defState yld sng undefined $
416− go r (step acc a)
417− in foldStream defState yieldk single stop m1
418−
419−-- | Strict left associative fold.
420−{-# INLINE foldl' #-}
421−foldl' :: (IsStream t, Monad m) => (b -> a -> b) -> b -> t m a -> m b
422−foldl' step begin = foldlx' step begin id
423−
424-- XXX replace the recursive "go" with explicit continuations.
386-- XXX replace the recursive "go" with explicit continuations.
425-- | Like 'foldx', but with a monadic step function.
387-- | Like 'foldx', but with a monadic step function.
426{-# INLINABLE foldlMx' #-}
388{-# INLINABLE foldlMx' #-}
src/Streamly/Internal/Data/Stream/StreamK/Type.hs+44
@@ -33,6 +33,8 @@ module Streamly.Internal.Data.Stream.StreamK.Type
33 -- * Elimination
33 -- * Elimination
34 , foldStream
34 , foldStream
35 , foldStreamShared
35 , foldStreamShared
36+ , foldl'
37+ , foldlx'
36
38
37 -- * foldr/build
39 -- * foldr/build
38 , foldrM
40 , foldrM
@@ -755,6 +757,48 @@ foldrMKWith f step acc g = go g
755 foldrM step acc (sharedM g) = foldrMKWith foldStreamShared step acc g
757 foldrM step acc (sharedM g) = foldrMKWith foldStreamShared step acc g
756 #-}
758 #-}
757
759
760+------------------------------------------------------------------------------
761+-- Left fold
762+------------------------------------------------------------------------------
763+
764+-- | Strict left fold with an extraction function. Like the standard strict
765+-- left fold, but applies a user supplied extraction function (the third
766+-- argument) to the folded value at the end. This is designed to work with the
767+-- @foldl@ library. The suffix @x@ is a mnemonic for extraction.
768+--
769+-- Note that the accumulator is always evaluated including the initial value.
770+{-# INLINE foldlx' #-}
771+foldlx' :: forall t m a b x. (IsStream t, Monad m)
772+ => (x -> a -> x) -> x -> (x -> b) -> t m a -> m b
773+foldlx' step begin done m = get $ go m begin
774+ where
775+ {-# NOINLINE get #-}
776+ get :: t m x -> m b
777+ get m1 =
778+ -- XXX we are not strictly evaluating the accumulator here. Is this
779+ -- okay?
780+ let single = return . done
781+ -- XXX this is foldSingleton. why foldStreamShared?
782+ in foldStreamShared undefined undefined single undefined m1
783+
784+ -- Note, this can be implemented by making a recursive call to "go",
785+ -- however that is more expensive because of unnecessary recursion
786+ -- that cannot be tail call optimized. Unfolding recursion explicitly via
787+ -- continuations is much more efficient.
788+ go :: t m a -> x -> t m x
789+ go m1 !acc = mkStream $ \_ yld sng _ ->
790+ let stop = sng acc
791+ single a = sng $ step acc a
792+ -- XXX this is foldNonEmptyStream
793+ yieldk a r = foldStream defState yld sng undefined $
794+ go r (step acc a)
795+ in foldStream defState yieldk single stop m1
796+
797+-- | Strict left associative fold.
798+{-# INLINE foldl' #-}
799+foldl' :: (IsStream t, Monad m) => (b -> a -> b) -> b -> t m a -> m b
800+foldl' step begin = foldlx' step begin id
801+
758------------------------------------------------------------------------------
802------------------------------------------------------------------------------
759-- Semigroup
803-- Semigroup
760------------------------------------------------------------------------------
804------------------------------------------------------------------------------