on master help sign in
Remove dependency of Array/Types on StreamK

It can depend on StreamK/Type but not on the high level StreamK module.

commit8af89ddffe1b76ac310da5e684c76eb584c63ec3 authorHarendra Kumar <harendra@composewell.com> dateFri 22 Jan 2021, 04:22 (6 years ago) parentda62ada
3 files changed+44−39
src/Streamly/Internal/Data/Array/Storable/Foreign/Mut/Types.hs−1 src/Streamly/Internal/Data/Stream/StreamK.hs−38 src/Streamly/Internal/Data/Stream/StreamK/Type.hs+44
src/Streamly/Internal/Data/Array/Storable/Foreign/Mut/Types.hs−1
@@ -123,7 +123,6 @@ import qualified Data.Foldable as F
123123import qualified GHC.Exts as Exts
124124import qualified Streamly.Internal.Data.Fold.Types as FL
125125import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
126−import qualified Streamly.Internal.Data.Stream.StreamK as K (foldl')
127126import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
128127import qualified Streamly.Internal.Foreign.Malloc as Malloc
129128
src/Streamly/Internal/Data/Stream/StreamK.hs−38
@@ -383,44 +383,6 @@ foldr1 step m = do
383383 yieldk a r = fmap (step p) (go a r)
384384 in foldStream defState yieldk single stp m1
385385
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−
424386-- XXX replace the recursive "go" with explicit continuations.
425387-- | Like 'foldx', but with a monadic step function.
426388{-# INLINABLE foldlMx' #-}
src/Streamly/Internal/Data/Stream/StreamK/Type.hs+44
@@ -33,6 +33,8 @@ module Streamly.Internal.Data.Stream.StreamK.Type
3333 -- * Elimination
3434 , foldStream
3535 , foldStreamShared
36+ , foldl'
37+ , foldlx'
3638
3739 -- * foldr/build
3840 , foldrM
@@ -755,6 +757,48 @@ foldrMKWith f step acc g = go g
755757 foldrM step acc (sharedM g) = foldrMKWith foldStreamShared step acc g
756758 #-}
757759
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+
758802------------------------------------------------------------------------------
759803-- Semigroup
760804------------------------------------------------------------------------------