on master help sign in
impl StreamD versions of max/min and optimize StreamK versions
commit9eedc703851ef1ec8c20601a6edbabde4766b524 authorHarendra Kumar <harendra.kumar@gmail.com> dateTue 26 Jun 2018, 19:43 (8 years ago) parentc7d2f9e
3 files changed+93−30
src/Streamly/Prelude.hs+6−28 src/Streamly/Streams/StreamD.hs+39−1 src/Streamly/Streams/StreamK.hs+48−1
src/Streamly/Prelude.hs+6−28
@@ -89,15 +89,15 @@ module Streamly.Prelude
8989 , foldxM
9090
9191 -- ** Specialized Folds
92− , all
93− , any
92+ , null
9493 , head
9594 , tail
9695 , last
97− , null
98− , length
9996 , elem
10097 , notElem
98+ , length
99+ , all
100+ , any
101101 , maximum
102102 , minimum
103103 , sum
@@ -591,41 +591,19 @@ notElem e m = go (toStream m)
591591length :: Monad m => SerialT m a -> m Int
592592length = foldl' (\n _ -> n + 1) 0
593593
594−-- XXX replace the recursive "go" with continuation
595594-- | Determine the minimum element in a stream.
596595--
597596-- @since 0.1.0
598597{-# INLINE minimum #-}
599598minimum :: (Monad m, Ord a) => SerialT m a -> m (Maybe a)
600−minimum m = go Nothing (toStream m)
601− where
602− go res m1 =
603− let stop = return res
604− single a = return $ min_ a res
605− yieldk a r = go (min_ a res) r
606− in (K.unStream m1) Nothing stop single yieldk
607−
608− min_ a res = case res of
609− Nothing -> Just a
610− Just e -> Just $ min a e
599+minimum m = S.minimum (toStreamS m)
611600
612−-- XXX replace the recursive "go" with continuation
613601-- | Determine the maximum element in a stream.
614602--
615603-- @since 0.1.0
616604{-# INLINE maximum #-}
617605maximum :: (Monad m, Ord a) => SerialT m a -> m (Maybe a)
618−maximum m = go Nothing (toStream m)
619− where
620− go res m1 =
621− let stop = return res
622− single a = return $ max_ a res
623− yieldk a r = go (max_ a res) r
624− in (K.unStream m1) Nothing stop single yieldk
625−
626− max_ a res = case res of
627− Nothing -> Just a
628− Just e -> Just $ max a e
606+maximum m = S.maximum (toStreamS m)
629607
630608------------------------------------------------------------------------------
631609-- Map and Fold
src/Streamly/Streams/StreamD.hs+39−1
@@ -75,6 +75,8 @@ module Streamly.Streams.StreamD
7575 -- ** Specialized Folds
7676 , runStream
7777 , last
78+ , maximum
79+ , minimum
7880
7981 -- ** Map and Fold
8082 , mapM_
@@ -112,7 +114,7 @@ import Data.Maybe (fromJust, isJust)
112114import GHC.Types ( SPEC(..) )
113115import Prelude
114116 hiding (map, mapM, mapM_, repeat, foldr, last, take, filter,
115− takeWhile, drop, dropWhile)
117+ takeWhile, drop, dropWhile, maximum, minimum)
116118
117119import Streamly.SVar (MonadAsync)
118120import qualified Streamly.Streams.StreamK as K
@@ -308,6 +310,42 @@ runStream (Stream step state) = go SPEC state
308310last :: Monad m => Stream m a -> m (Maybe a)
309311last = foldl' (\_ y -> Just y) Nothing
310312
313+{-# INLINE_NORMAL maximum #-}
314+maximum :: (Monad m, Ord a) => Stream m a -> m (Maybe a)
315+maximum (Stream step state) = go Nothing state
316+ where
317+ go Nothing st = do
318+ r <- step st
319+ case r of
320+ Yield x s -> go (Just x) s
321+ Stop -> return Nothing
322+ go (Just acc) st = do
323+ r <- step st
324+ case r of
325+ Yield x s ->
326+ if acc <= x
327+ then go (Just x) s
328+ else go (Just acc) s
329+ Stop -> return (Just acc)
330+
331+{-# INLINE_NORMAL minimum #-}
332+minimum :: (Monad m, Ord a) => Stream m a -> m (Maybe a)
333+minimum (Stream step state) = go Nothing state
334+ where
335+ go Nothing st = do
336+ r <- step st
337+ case r of
338+ Yield x s -> go (Just x) s
339+ Stop -> return Nothing
340+ go (Just acc) st = do
341+ r <- step st
342+ case r of
343+ Yield x s ->
344+ if acc <= x
345+ then go (Just acc) s
346+ else go (Just x) s
347+ Stop -> return (Just acc)
348+
311349------------------------------------------------------------------------------
312350-- Map and Fold
313351------------------------------------------------------------------------------
src/Streamly/Streams/StreamK.hs+48−1
@@ -78,6 +78,8 @@ module Streamly.Streams.StreamK
7878 , all
7979 , any
8080 , last
81+ , minimum
82+ , maximum
8183
8284 -- ** Map and Fold
8385 , mapM_
@@ -126,7 +128,8 @@ import Control.Monad.Trans.Class (MonadTrans(lift))
126128import Data.Semigroup (Semigroup(..))
127129import Prelude
128130 hiding (foldl, foldr, last, map, mapM, mapM_, repeat, sequence,
129− take, filter, all, any, takeWhile, drop, dropWhile)
131+ take, filter, all, any, takeWhile, drop, dropWhile, minimum,
132+ maximum)
130133import qualified Prelude
131134
132135import Streamly.SVar
@@ -543,6 +546,50 @@ any p m = go (toStream m)
543546last :: (IsStream t, Monad m) => t m a -> m (Maybe a)
544547last = foldx (\_ y -> Just y) Nothing id
545548
549+{-# INLINE minimum #-}
550+minimum :: (IsStream t, Monad m, Ord a) => t m a -> m (Maybe a)
551+minimum m = go Nothing (toStream m)
552+ where
553+ go Nothing m1 =
554+ let stop = return Nothing
555+ single a = return (Just a)
556+ yieldk a r = go (Just a) r
557+ in unStream m1 Nothing stop single yieldk
558+
559+ go (Just res) m1 =
560+ let stop = return (Just res)
561+ single a =
562+ if res <= a
563+ then return (Just res)
564+ else return (Just a)
565+ yieldk a r =
566+ if res <= a
567+ then go (Just res) r
568+ else go (Just a) r
569+ in unStream m1 Nothing stop single yieldk
570+
571+{-# INLINE maximum #-}
572+maximum :: (IsStream t, Monad m, Ord a) => t m a -> m (Maybe a)
573+maximum m = go Nothing (toStream m)
574+ where
575+ go Nothing m1 =
576+ let stop = return Nothing
577+ single a = return (Just a)
578+ yieldk a r = go (Just a) r
579+ in unStream m1 Nothing stop single yieldk
580+
581+ go (Just res) m1 =
582+ let stop = return (Just res)
583+ single a =
584+ if res <= a
585+ then return (Just a)
586+ else return (Just res)
587+ yieldk a r =
588+ if res <= a
589+ then go (Just a) r
590+ else go (Just res) r
591+ in unStream m1 Nothing stop single yieldk
592+
546593------------------------------------------------------------------------------
547594-- Map and Fold
548595------------------------------------------------------------------------------