on master help sign in
optimize dropWhile and add tests
3cca597Harendra Kumar
src/Streamly/Prelude.hs+2−5
@@ -216,10 +216,6 @@ take n m = fromStream $ go n (toStream m)
216 yield a (Just x) = yld a (Just (go (n1 - 1) x))
216 yield a (Just x) = yld a (Just (go (n1 - 1) x))
217 in if n1 <= 0 then stp else (runStream m1) ctx stp yield
217 in if n1 <= 0 then stp else (runStream m1) ctx stp yield
218
218
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−
223-- | Include only those elements that pass a predicate.
219-- | Include only those elements that pass a predicate.
224{-# INLINE filter #-}
220{-# INLINE filter #-}
225filter :: Streaming t => (a -> Bool) -> t m a -> t m a
221filter :: Streaming t => (a -> Bool) -> t m a -> t m a
@@ -256,13 +252,14 @@ drop n m = fromStream $ go n (toStream m)
256
252
257-- | Drop elements in the stream as long as the predicate succeeds and then
253-- | Drop elements in the stream as long as the predicate succeeds and then
258-- take the rest of the stream.
254-- take the rest of the stream.
255+{-# INLINE dropWhile #-}
259dropWhile :: Streaming t => (a -> Bool) -> t m a -> t m a
256dropWhile :: Streaming t => (a -> Bool) -> t m a -> t m a
260dropWhile p m = fromStream $ go (toStream m)
257dropWhile p m = fromStream $ go (toStream m)
261 where
258 where
262 go m1 = Stream $ \ctx stp yld ->
259 go m1 = Stream $ \ctx stp yld ->
263 let yield a Nothing | p a = stp
260 let yield a Nothing | p a = stp
264 | otherwise = yld a Nothing
261 | 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
266 | otherwise = yld a (Just x)
263 | otherwise = yld a (Just x)
267 in (runStream m1) ctx stp yield
264 in (runStream m1) ctx stp yield
268
265
test/Main.hs+6
@@ -622,6 +622,12 @@ transformOps f = do
622 it "drop none" $ testOp (A.drop 0) (drop 0)
622 it "drop none" $ testOp (A.drop 0) (drop 0)
623 it "drop 5" $ testOp (A.drop 5) (drop 5)
623 it "drop 5" $ testOp (A.drop 5) (drop 5)
624
624
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+
625 it "filter all out" $ testOp (A.filter (> 10)) (filter (> 10))
631 it "filter all out" $ testOp (A.filter (> 10)) (filter (> 10))
626 it "filter all in" $ testOp (A.filter (<= 10)) (filter (<= 10))
632 it "filter all in" $ testOp (A.filter (<= 10)) (filter (<= 10))
627 it "filter even" $ testOp (A.filter even) (filter even)
633 it "filter even" $ testOp (A.filter even) (filter even)