on master help sign in
optimize dropWhile and add tests
commit3cca5971e0a6a6355720264a776d6238908d1484 authorHarendra Kumar <harendra.kumar@gmail.com> dateFri 9 Mar 2018, 14:28 (9 years ago) parent25b3a3e
2 files changed+8−5
src/Streamly/Prelude.hs+2−5 test/Main.hs+6
src/Streamly/Prelude.hs+2−5
@@ -216,10 +216,6 @@ take n m = fromStream $ go n (toStream m)
216216 yield a (Just x) = yld a (Just (go (n1 - 1) x))
217217 in if n1 <= 0 then stp else (runStream m1) ctx stp yield
218218
219−-- XXX This is not as efficient as it could be. We need a short circuiting at
220−-- a lower level. Compare with simple-conduit, filtering there cuts down time
221−-- due to short circuting whereas the time spent remains the same here.
222−
223219-- | Include only those elements that pass a predicate.
224220{-# INLINE filter #-}
225221filter :: Streaming t => (a -> Bool) -> t m a -> t m a
@@ -256,13 +252,14 @@ drop n m = fromStream $ go n (toStream m)
256252
257253-- | Drop elements in the stream as long as the predicate succeeds and then
258254-- take the rest of the stream.
255+{-# INLINE dropWhile #-}
259256dropWhile :: Streaming t => (a -> Bool) -> t m a -> t m a
260257dropWhile p m = fromStream $ go (toStream m)
261258 where
262259 go m1 = Stream $ \ctx stp yld ->
263260 let yield a Nothing | p a = stp
264261 | otherwise = yld a Nothing
265− yield a (Just x) | p a = (runStream (go x)) ctx stp yield
262+ yield a (Just x) | p a = (runStream x) ctx stp yield
266263 | otherwise = yld a (Just x)
267264 in (runStream m1) ctx stp yield
268265
test/Main.hs+6
@@ -622,6 +622,12 @@ transformOps f = do
622622 it "drop none" $ testOp (A.drop 0) (drop 0)
623623 it "drop 5" $ testOp (A.drop 5) (drop 5)
624624
625+ it "dropWhile true" $ testOp (A.dropWhile (const True))
626+ (dropWhile (const True))
627+ it "dropWhile false" $ testOp (A.dropWhile (const False))
628+ (dropWhile (const False))
629+ it "dropWhile < 5" $ testOp (A.dropWhile (< 5)) (dropWhile (< 5))
630+
625631 it "filter all out" $ testOp (A.filter (> 10)) (filter (> 10))
626632 it "filter all in" $ testOp (A.filter (<= 10)) (filter (<= 10))
627633 it "filter even" $ testOp (A.filter even) (filter even)