on master help sign in
implement elem/notElem/all/any for StreamD
commit9b78a64f80ce5ae047f811dfcc75d2f23c6426ea authorHarendra Kumar <harendra.kumar@gmail.com> dateWed 27 Jun 2018, 01:04 (8 years ago) parent9eedc70
5 files changed+140−54
benchmark/Linear.hs+7−2 benchmark/LinearOps.hs+13−3 src/Streamly/Prelude.hs+39−47 src/Streamly/Streams/StreamD.hs+58−1 src/Streamly/Streams/StreamK.hs+23−1
benchmark/Linear.hs+7−2
@@ -60,12 +60,17 @@ main = do
6060 , benchIO "foldr" Ops.foldr
6161 , benchIO "foldrM" Ops.foldrM
6262 , benchIO "foldl'" Ops.foldl
63+
6364 , benchIO "last" Ops.last
6465 , benchIO "length" Ops.length
65− , benchIO "sum" Ops.sum
66− , benchIO "product" Ops.product
66+ , benchIO "elem" Ops.elem
67+ , benchIO "notElem" Ops.notElem
68+ , benchIO "all" Ops.all
69+ , benchIO "any" Ops.any
6770 , benchIO "maximum" Ops.maximum
6871 , benchIO "minimum" Ops.minimum
72+ , benchIO "sum" Ops.sum
73+ , benchIO "product" Ops.product
6974 ]
7075 , bgroup "transformation"
7176 [ benchIO "scan" Ops.scan
benchmark/LinearOps.hs+13−3
@@ -11,7 +11,7 @@ module LinearOps where
1111
1212import Prelude
1313 (Monad, Int, (+), ($), (.), return, fmap, even, (>), (<=),
14− subtract, undefined, Maybe(..), odd)
14+ subtract, undefined, Maybe(..), odd, Bool)
1515
1616import qualified Streamly as S
1717import qualified Streamly.Prelude as S
@@ -73,6 +73,12 @@ last, minimum, maximum :: Monad m => Stream m Int -> m (Maybe Int)
7373{-# INLINE product #-}
7474foldl, length, sum, product :: Monad m => Stream m Int -> m Int
7575
76+{-# INLINE all #-}
77+{-# INLINE any #-}
78+{-# INLINE elem #-}
79+{-# INLINE notElem #-}
80+elem, notElem, all, any :: Monad m => Stream m Int -> m Bool
81+
7682{-# INLINE toNull #-}
7783toNull :: Monad m => (t m Int -> S.SerialT m Int) -> t m Int -> m ()
7884
@@ -163,11 +169,15 @@ foldr = S.foldr (:) []
163169foldrM = S.foldrM (\a xs -> return (a : xs)) []
164170foldl = S.foldl' (+) 0
165171last = S.last
172+elem = S.elem maxValue
173+notElem = S.notElem maxValue
166174length = S.length
167−sum = S.sum
168−product = S.product
175+all = S.all (<= maxValue)
176+any = S.any (> maxValue)
169177maximum = S.maximum
170178minimum = S.minimum
179+sum = S.sum
180+product = S.product
171181
172182-------------------------------------------------------------------------------
173183-- Transformation
src/Streamly/Prelude.hs+39−47
@@ -497,31 +497,15 @@ foldlM' step begin m = S.foldlM' step begin $ toStreamS m
497497-- Specialized folds
498498------------------------------------------------------------------------------
499499
500−-- | Determine whether all elements of a stream satisfy a predicate.
501−--
502−-- @since 0.1.0
503−all :: Monad m => (a -> Bool) -> SerialT m a -> m Bool
504−all = K.all
505−
506−-- | Determine whether any of the elements of a stream satisfy a predicate.
507−--
508−-- @since 0.1.0
509−any :: Monad m => (a -> Bool) -> SerialT m a -> m Bool
510−any = K.any
511−
512−-- | Determine the sum of all elements of a stream of numbers
513−--
514−-- @since 0.1.0
515−{-# INLINE sum #-}
516−sum :: (Monad m, Num a) => SerialT m a -> m a
517−sum = foldl' (+) 0
518−
519−-- | Determine the product of all elements of a stream of numbers
500+-- | Determine whether the stream is empty.
520501--
521502-- @since 0.1.1
522−{-# INLINE product #-}
523−product :: (Monad m, Num a) => SerialT m a -> m a
524−product = foldl' (*) 1
503+null :: Monad m => SerialT m a -> m Bool
504+null m =
505+ let stop = return True
506+ single _ = return False
507+ yieldk _ _ = return False
508+ in (K.unStream (toStream m)) Nothing stop single yieldk
525509
526510-- | Extract the first element of the stream, if any.
527511--
@@ -550,39 +534,19 @@ tail m =
550534last :: Monad m => SerialT m a -> m (Maybe a)
551535last m = S.last $ toStreamS m
552536
553−-- | Determine whether the stream is empty.
554−--
555−-- @since 0.1.1
556−null :: Monad m => SerialT m a -> m Bool
557−null m =
558− let stop = return True
559− single _ = return False
560− yieldk _ _ = return False
561− in (K.unStream (toStream m)) Nothing stop single yieldk
562−
563537-- | Determine whether an element is present in the stream.
564538--
565539-- @since 0.1.0
540+{-# INLINE elem #-}
566541elem :: (Monad m, Eq a) => a -> SerialT m a -> m Bool
567−elem e m = go (toStream m)
568− where
569− go m1 =
570− let stop = return False
571− single a = return (a == e)
572− yieldk a r = if a == e then return True else go r
573− in (K.unStream m1) Nothing stop single yieldk
542+elem e m = S.elem e (toStreamS m)
574543
575544-- | Determine whether an element is not present in the stream.
576545--
577546-- @since 0.1.0
547+{-# INLINE notElem #-}
578548notElem :: (Monad m, Eq a) => a -> SerialT m a -> m Bool
579−notElem e m = go (toStream m)
580− where
581− go m1 =
582− let stop = return True
583− single a = return (a /= e)
584− yieldk a r = if a == e then return False else go r
585− in (K.unStream m1) Nothing stop single yieldk
549+notElem e m = S.notElem e (toStreamS m)
586550
587551-- | Determine the length of the stream.
588552--
@@ -591,6 +555,34 @@ notElem e m = go (toStream m)
591555length :: Monad m => SerialT m a -> m Int
592556length = foldl' (\n _ -> n + 1) 0
593557
558+-- | Determine whether all elements of a stream satisfy a predicate.
559+--
560+-- @since 0.1.0
561+{-# INLINE all #-}
562+all :: Monad m => (a -> Bool) -> SerialT m a -> m Bool
563+all p m = S.all p (toStreamS m)
564+
565+-- | Determine whether any of the elements of a stream satisfy a predicate.
566+--
567+-- @since 0.1.0
568+{-# INLINE any #-}
569+any :: Monad m => (a -> Bool) -> SerialT m a -> m Bool
570+any p m = S.any p (toStreamS m)
571+
572+-- | Determine the sum of all elements of a stream of numbers
573+--
574+-- @since 0.1.0
575+{-# INLINE sum #-}
576+sum :: (Monad m, Num a) => SerialT m a -> m a
577+sum = foldl' (+) 0
578+
579+-- | Determine the product of all elements of a stream of numbers
580+--
581+-- @since 0.1.1
582+{-# INLINE product #-}
583+product :: (Monad m, Num a) => SerialT m a -> m a
584+product = foldl' (*) 1
585+
594586-- | Determine the minimum element in a stream.
595587--
596588-- @since 0.1.0
src/Streamly/Streams/StreamD.hs+58−1
@@ -75,6 +75,10 @@ module Streamly.Streams.StreamD
7575 -- ** Specialized Folds
7676 , runStream
7777 , last
78+ , elem
79+ , notElem
80+ , all
81+ , any
7882 , maximum
7983 , minimum
8084
@@ -114,7 +118,8 @@ import Data.Maybe (fromJust, isJust)
114118import GHC.Types ( SPEC(..) )
115119import Prelude
116120 hiding (map, mapM, mapM_, repeat, foldr, last, take, filter,
117− takeWhile, drop, dropWhile, maximum, minimum)
121+ takeWhile, drop, dropWhile, all, any, maximum, minimum, elem,
122+ notElem)
118123
119124import Streamly.SVar (MonadAsync)
120125import qualified Streamly.Streams.StreamK as K
@@ -310,6 +315,58 @@ runStream (Stream step state) = go SPEC state
310315last :: Monad m => Stream m a -> m (Maybe a)
311316last = foldl' (\_ y -> Just y) Nothing
312317
318+{-# INLINE_NORMAL elem #-}
319+elem :: (Monad m, Eq a) => a -> Stream m a -> m Bool
320+elem e (Stream step state) = go state
321+ where
322+ go st = do
323+ r <- step st
324+ case r of
325+ Yield x s ->
326+ if x == e
327+ then return True
328+ else go s
329+ Stop -> return False
330+
331+{-# INLINE_NORMAL notElem #-}
332+notElem :: (Monad m, Eq a) => a -> Stream m a -> m Bool
333+notElem e (Stream step state) = go state
334+ where
335+ go st = do
336+ r <- step st
337+ case r of
338+ Yield x s ->
339+ if x == e
340+ then return False
341+ else go s
342+ Stop -> return True
343+
344+{-# INLINE_NORMAL all #-}
345+all :: Monad m => (a -> Bool) -> Stream m a -> m Bool
346+all p (Stream step state) = go state
347+ where
348+ go st = do
349+ r <- step st
350+ case r of
351+ Yield x s ->
352+ if p x
353+ then go s
354+ else return False
355+ Stop -> return True
356+
357+{-# INLINE_NORMAL any #-}
358+any :: Monad m => (a -> Bool) -> Stream m a -> m Bool
359+any p (Stream step state) = go state
360+ where
361+ go st = do
362+ r <- step st
363+ case r of
364+ Yield x s ->
365+ if p x
366+ then return True
367+ else go s
368+ Stop -> return False
369+
313370{-# INLINE_NORMAL maximum #-}
314371maximum :: (Monad m, Ord a) => Stream m a -> m (Maybe a)
315372maximum (Stream step state) = go Nothing state
src/Streamly/Streams/StreamK.hs+23−1
@@ -75,6 +75,8 @@ module Streamly.Streams.StreamK
7575
7676 -- ** Specialized Folds
7777 , runStream
78+ , elem
79+ , notElem
7880 , all
7981 , any
8082 , last
@@ -129,7 +131,7 @@ import Data.Semigroup (Semigroup(..))
129131import Prelude
130132 hiding (foldl, foldr, last, map, mapM, mapM_, repeat, sequence,
131133 take, filter, all, any, takeWhile, drop, dropWhile, minimum,
132− maximum)
134+ maximum, elem, notElem)
133135import qualified Prelude
134136
135137import Streamly.SVar
@@ -521,6 +523,26 @@ runStream m = go (toStream m)
521523 yieldk _ r = go (toStream r)
522524 in (unStream m1) Nothing stop single yieldk
523525
526+{-# INLINE elem #-}
527+elem :: (IsStream t, Monad m, Eq a) => a -> t m a -> m Bool
528+elem e m = go (toStream m)
529+ where
530+ go m1 =
531+ let stop = return False
532+ single a = return (a == e)
533+ yieldk a r = if a == e then return True else go r
534+ in (unStream m1) Nothing stop single yieldk
535+
536+{-# INLINE notElem #-}
537+notElem :: (IsStream t, Monad m, Eq a) => a -> t m a -> m Bool
538+notElem e m = go (toStream m)
539+ where
540+ go m1 =
541+ let stop = return True
542+ single a = return (a /= e)
543+ yieldk a r = if a == e then return False else go r
544+ in (unStream m1) Nothing stop single yieldk
545+
524546all :: (IsStream t, Monad m) => (a -> Bool) -> t m a -> m Bool
525547all p m = go (toStream m)
526548 where