on master help sign in
impl StreamD versions of max/min and optimize StreamK versions
9eedc70Harendra Kumar
src/Streamly/Prelude.hs+6−28
@@ -89,15 +89,15 @@ module Streamly.Prelude
89 , foldxM
89 , foldxM
90
90
91 -- ** Specialized Folds
91 -- ** Specialized Folds
92− , all
92+ , null
93− , any
94 , head
93 , head
95 , tail
94 , tail
96 , last
95 , last
97− , null
98− , length
99 , elem
96 , elem
100 , notElem
97 , notElem
98+ , length
99+ , all
100+ , any
101 , maximum
101 , maximum
102 , minimum
102 , minimum
103 , sum
103 , sum
@@ -591,41 +591,19 @@ notElem e m = go (toStream m)
591length :: Monad m => SerialT m a -> m Int
591length :: Monad m => SerialT m a -> m Int
592length = foldl' (\n _ -> n + 1) 0
592length = foldl' (\n _ -> n + 1) 0
593
593
594−-- XXX replace the recursive "go" with continuation
595-- | Determine the minimum element in a stream.
594-- | Determine the minimum element in a stream.
596--
595--
597-- @since 0.1.0
596-- @since 0.1.0
598{-# INLINE minimum #-}
597{-# INLINE minimum #-}
599minimum :: (Monad m, Ord a) => SerialT m a -> m (Maybe a)
598minimum :: (Monad m, Ord a) => SerialT m a -> m (Maybe a)
600−minimum m = go Nothing (toStream m)
599+minimum m = S.minimum (toStreamS 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
611
600
612−-- XXX replace the recursive "go" with continuation
613-- | Determine the maximum element in a stream.
601-- | Determine the maximum element in a stream.
614--
602--
615-- @since 0.1.0
603-- @since 0.1.0
616{-# INLINE maximum #-}
604{-# INLINE maximum #-}
617maximum :: (Monad m, Ord a) => SerialT m a -> m (Maybe a)
605maximum :: (Monad m, Ord a) => SerialT m a -> m (Maybe a)
618−maximum m = go Nothing (toStream m)
606+maximum m = S.maximum (toStreamS 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
629
607
630------------------------------------------------------------------------------
608------------------------------------------------------------------------------
631-- Map and Fold
609-- Map and Fold
src/Streamly/Streams/StreamD.hs+39−1
@@ -75,6 +75,8 @@ module Streamly.Streams.StreamD
75 -- ** Specialized Folds
75 -- ** Specialized Folds
76 , runStream
76 , runStream
77 , last
77 , last
78+ , maximum
79+ , minimum
78
80
79 -- ** Map and Fold
81 -- ** Map and Fold
80 , mapM_
82 , mapM_
@@ -112,7 +114,7 @@ import Data.Maybe (fromJust, isJust)
112import GHC.Types ( SPEC(..) )
114import GHC.Types ( SPEC(..) )
113import Prelude
115import Prelude
114 hiding (map, mapM, mapM_, repeat, foldr, last, take, filter,
116 hiding (map, mapM, mapM_, repeat, foldr, last, take, filter,
115− takeWhile, drop, dropWhile)
117+ takeWhile, drop, dropWhile, maximum, minimum)
116
118
117import Streamly.SVar (MonadAsync)
119import Streamly.SVar (MonadAsync)
118import qualified Streamly.Streams.StreamK as K
120import qualified Streamly.Streams.StreamK as K
@@ -308,6 +310,42 @@ runStream (Stream step state) = go SPEC state
308last :: Monad m => Stream m a -> m (Maybe a)
310last :: Monad m => Stream m a -> m (Maybe a)
309last = foldl' (\_ y -> Just y) Nothing
311last = foldl' (\_ y -> Just y) Nothing
310
312
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+
311------------------------------------------------------------------------------
349------------------------------------------------------------------------------
312-- Map and Fold
350-- Map and Fold
313------------------------------------------------------------------------------
351------------------------------------------------------------------------------
src/Streamly/Streams/StreamK.hs+48−1
@@ -78,6 +78,8 @@ module Streamly.Streams.StreamK
78 , all
78 , all
79 , any
79 , any
80 , last
80 , last
81+ , minimum
82+ , maximum
81
83
82 -- ** Map and Fold
84 -- ** Map and Fold
83 , mapM_
85 , mapM_
@@ -126,7 +128,8 @@ import Control.Monad.Trans.Class (MonadTrans(lift))
126import Data.Semigroup (Semigroup(..))
128import Data.Semigroup (Semigroup(..))
127import Prelude
129import Prelude
128 hiding (foldl, foldr, last, map, mapM, mapM_, repeat, sequence,
130 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)
130import qualified Prelude
133import qualified Prelude
131
134
132import Streamly.SVar
135import Streamly.SVar
@@ -543,6 +546,50 @@ any p m = go (toStream m)
543last :: (IsStream t, Monad m) => t m a -> m (Maybe a)
546last :: (IsStream t, Monad m) => t m a -> m (Maybe a)
544last = foldx (\_ y -> Just y) Nothing id
547last = foldx (\_ y -> Just y) Nothing id
545
548
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+
546------------------------------------------------------------------------------
593------------------------------------------------------------------------------
547-- Map and Fold
594-- Map and Fold
548------------------------------------------------------------------------------
595------------------------------------------------------------------------------