on master help sign in
Expose groupsOf from Data.Stream (#2372)
commit1ab2878ecad6a12279f4f56e851763e06426c835 authorRanjeet Ranjan <ranjeetr@composewell.com> dateTue 4 Jul 2023, 23:07 (3 years ago) parentf1be90c
5 files changed+53
core/src/Streamly/Data/Stream.hs+1 core/src/Streamly/Internal/Data/Stream/StreamD/Type.hs+11 hie.yaml+2 test/Streamly/Test/Data/Stream.hs+33 test/streamly-tests.cabal+6
core/src/Streamly/Data/Stream.hs+1
@@ -431,6 +431,7 @@ module Streamly.Data.Stream
431431 -- >>> wordsBy p f = Stream.parseMany (Parser.wordBy p f)
432432 -- >>> groupsOf n f = Stream.foldMany (Fold.take n f)
433433 , foldMany -- XXX Rename to foldRepeat
434+ , groupsOf
434435 , parseMany
435436 , Array.chunksOf
436437
core/src/Streamly/Internal/Data/Stream/StreamD/Type.hs+11
@@ -1903,6 +1903,17 @@ foldMany (Fold fstep initial extract) (Stream step state) =
19031903 step' _ (FoldManyYield b next) = return $ Yield b next
19041904 step' _ FoldManyDone = return Stop
19051905
1906+-- | Group the input stream into groups of @n@ elements each and then fold each
1907+-- group using the provided fold function.
1908+--
1909+-- @groupsOf n f = foldMany (FL.take n f)@
1910+--
1911+-- >>> Stream.toList $ Stream.groupsOf 2 Fold.sum (Stream.enumerateFromTo 1 10)
1912+-- [3,7,11,15,19]
1913+--
1914+-- This can be considered as an n-fold version of 'take' where we apply
1915+-- 'take' repeatedly on the leftover stream until the stream exhausts.
1916+--
19061917{-# INLINE groupsOf #-}
19071918groupsOf :: Monad m => Int -> Fold m a b -> Stream m a -> Stream m b
19081919groupsOf n f = foldMany (FL.take n f)
hie.yaml+2
@@ -94,6 +94,8 @@ cradle:
9494 cabal:
9595 - path: "./test/Streamly/Test/Data/Array.hs"
9696 component: "test:Data.Array"
97+ - path: "./test/Streamly/Test/Data/Stream.hs"
98+ component: "test:Data.Stream"
9799 - path: "./test/Streamly/Test/Data/Array/Generic.hs"
98100 component: "test:Data.Array.Generic"
99101 - path: "./test/Streamly/Test/Data/Array/Mut.hs"
test/Streamly/Test/Data/Stream.hsnew file+33
@@ -0,0 +1,33 @@
1+-- |
2+-- Module : Streamly.Test.Data.Stream
3+-- Copyright : (c) 2019 Composewell technologies
4+-- License : BSD-3-Clause
5+-- Maintainer : streamly@composewell.com
6+-- Stability : experimental
7+-- Portability : GHC
8+
9+module Streamly.Test.Data.Stream (main) where
10+
11+import qualified Streamly.Data.Fold as Fold
12+import qualified Streamly.Data.Stream as Stream
13+
14+import Test.Hspec.QuickCheck
15+import Test.Hspec as H
16+
17+moduleName :: String
18+moduleName = "Data.Stream"
19+
20+testgroupsOf :: Expectation
21+testgroupsOf =
22+ Stream.toList
23+ (Stream.groupsOf 2 Fold.sum (Stream.enumerateFromTo 1 10))
24+ `shouldReturn` [3::Int, 7, 11, 15, 19]
25+
26+main :: IO ()
27+main =
28+ hspec $
29+ H.parallel $
30+ modifyMaxSuccess (const 1) $ do
31+ describe moduleName $ do
32+ describe "Repeated Fold" $ do
33+ prop "testgroupsOf" testgroupsOf
test/streamly-tests.cabal+6
@@ -241,6 +241,12 @@ test-suite Data.Array
241241 main-is: Streamly/Test/Data/Array.hs
242242 ghc-options: -main-is Streamly.Test.Data.Array.main
243243
244+test-suite Data.Stream
245+ import: test-options
246+ type: exitcode-stdio-1.0
247+ main-is: Streamly/Test/Data/Stream.hs
248+ ghc-options: -main-is Streamly.Test.Data.Stream.main
249+
244250test-suite Data.Array.Mut
245251 import: test-options
246252 type: exitcode-stdio-1.0