1{-# LANGUAGE BangPatterns #-}
3{-# LANGUAGE ConstraintKinds #-}
4{-# LANGUAGE ExistentialQuantification #-}
5{-# LANGUAGE FlexibleContexts #-}
6{-# LANGUAGE FlexibleInstances #-}
7{-# LANGUAGE MultiParamTypeClasses #-}
8{-# LANGUAGE PatternSynonyms #-}
9{-# LANGUAGE ViewPatterns #-}
10{-# LANGUAGE RankNTypes #-}
15-- Module : Streamly.Streams.StreamD
16-- Copyright : (c) 2018 Harendra Kumar
17-- Copyright : (c) Roman Leshchinskiy 2008-2010
20-- Maintainer : harendra.kumar@gmail.com
21-- Stability : experimental
24-- Direct style re-implementation of CPS style stream in StreamK module. The
25-- symbol or suffix 'D' in this module denotes the "Direct" style. GHC is able
26-- to INLINE and fuse direct style better, providing better performance than
30-- import qualified Streamly.Streams.StreamD as D
33-- Some of the functions in this file have been adapted from the vector
34-- library, https://hackage.haskell.org/package/vector.
36module Streamly.Streams.StreamD
54 -- ** Specialized Generation
55 -- | Generate a monadic stream from a seed.
78 -- | Transform an input structure into a stream.
79 -- | Direct style stream does not support @fromFoldable@.
95 -- ** Specialized Folds
126 -- | Transform a stream into another type.
132 -- ** By folding (scans)
189 -- * Transformation comprehensions
194import Data.Maybe (fromJust, isJust)
195import GHC.Types ( SPEC(..) )
197 hiding (map, mapM, mapM_, repeat, foldr, last, take, filter,
198 takeWhile, drop, dropWhile, all, any, maximum, minimum, elem,
199 notElem, null, head, tail, zipWith, lookup, foldr1, sequence,
200 (!!), scanl, scanl1, concatMap, replicate, enumFromTo)
202import Streamly.SVar (MonadAsync, State(..), defState, adaptState)
204import qualified Streamly.Streams.StreamK as K
206------------------------------------------------------------------------------
207-- The direct style stream type
208------------------------------------------------------------------------------
210-- | A stream is a succession of 'Step's. A 'Yield' produces a single value and
211-- the next state of the stream. 'Stop' indicates there are no more values in
213data Step s a = Yield a s | Skip s | Stop
215instance Functor (Step s) where
217 fmap f (Yield x s) = Yield (f x) s
218 fmap _ (Skip s) = Skip s
222-- | A stream consists of a step function that generates the next step given a
223-- current state, and the current state.
225 forall s. UnStream (State K.Stream m a -> s -> m (Step s a)) s
227-- Do not use the UnStream constructor directly, use the pattern synonym
228-- instead. XXX we should perhaps put the type in a separate module and do not
229-- export the constructor.
231detachStream :: Stream m a -> Stream m a
232detachStream (UnStream step state) = UnStream step' state
233 where step' gst = step (adaptState gst)
235pattern Stream :: (State K.Stream m a -> s -> m (Step s a)) -> s -> Stream m a
236pattern Stream step state <- (detachStream -> UnStream step state)
237 where Stream = UnStream
239#if __GLASGOW_HASKELL__ >= 802
240{-# COMPLETE Stream #-}
243------------------------------------------------------------------------------
245------------------------------------------------------------------------------
247-- | An empty 'Stream'.
248{-# INLINE_NORMAL nil #-}
249nil :: Monad m => Stream m a
250nil = Stream (\_ _ -> return Stop) ()
252-- | Can fuse but has O(n^2) complexity.
253{-# INLINE_NORMAL cons #-}
254cons :: Monad m => a -> Stream m a -> Stream m a
255cons x (Stream step state) = Stream step1 Nothing
257 {-# INLINE_LATE step1 #-}
258 step1 _ Nothing = return $ Yield x (Just state)
259 step1 gst (Just st) = do
263 Yield a s -> Yield a (Just s)
264 Skip s -> Skip (Just s)
267-------------------------------------------------------------------------------
269-------------------------------------------------------------------------------
271-- Does not fuse, has the same performance as the StreamK version.
272{-# INLINE_NORMAL uncons #-}
273uncons :: Monad m => Stream m a -> m (Maybe (a, Stream m a))
274uncons (Stream step state) = go state
277 r <- step defState st
279 Yield x s -> return $ Just (x, Stream step s)
281 Stop -> return Nothing
283------------------------------------------------------------------------------
284-- Generation by unfold
285------------------------------------------------------------------------------
287{-# INLINE_NORMAL unfoldrM #-}
288unfoldrM :: Monad m => (s -> m (Maybe (a, s))) -> s -> Stream m a
289unfoldrM next state = Stream step state
291 {-# INLINE_LATE step #-}
295 Just (x, s) -> Yield x s
298{-# INLINE_LATE unfoldr #-}
299unfoldr :: Monad m => (s -> Maybe (a, s)) -> s -> Stream m a
300unfoldr f = unfoldrM (return . f)
302------------------------------------------------------------------------------
303-- Specialized Generation
304------------------------------------------------------------------------------
306repeat :: Monad m => a -> Stream m a
307repeat x = Stream (\_ _ -> return $ Yield x ()) ()
309{-# INLINE_NORMAL replicateM #-}
310replicateM :: Monad m => Int -> m a -> Stream m a
311replicateM n p = Stream step n
313 {-# INLINE_LATE step #-}
314 step _ i | i <= 0 = return Stop
317 return $ Yield x (i - 1)
319{-# INLINE_NORMAL replicate #-}
320replicate :: Monad m => Int -> a -> Stream m a
321replicate n x = replicateM n (return x)
323-- This would not work properly for floats, therefore we put an Integral
325-- | Can be used to enumerate unbounded integrals. This does not check for
326-- overflow or underflow for bounded integrals.
327{-# INLINE_NORMAL intFromStep #-}
328intFromStep :: (Integral a, Monad m) => a -> a -> Stream m a
329intFromStep from stride =
330 from `seq` stride `seq` Stream step from
332 {-# INLINE_LATE step #-}
333 step _ !x = return $ Yield x $! (x + stride)
335-- We are assuming that "to" is constrained by the type to be within
337{-# INLINE intFromTo #-}
338intFromTo :: (Monad m, Integral a) => a -> a -> Stream m a
339intFromTo from to = takeWhile (<= to) $ intFromStep from 1
341{-# INLINE intFrom #-}
342intFrom :: (Monad m, Integral a, Bounded a) => a -> Stream m a
343intFrom from = intFromTo from maxBound
345data EnumState a = EnumInit | EnumYield a a a | EnumStop
347{-# INLINE_NORMAL intFromThenToUp #-}
348intFromThenToUp :: (Monad m, Integral a) => a -> a -> a -> Stream m a
349intFromThenToUp from next to = Stream step EnumInit
351 {-# INLINE_LATE step #-}
357 else Yield from EnumStop
358 else -- from <= next <= to
359 let stride = next - from
360 in Skip $ EnumYield from stride (to - stride)
362 step _ (EnumYield x stride toMinus) =
365 then Yield x EnumStop
366 else Yield x $ EnumYield (x + stride) stride toMinus
368 step _ EnumStop = return Stop
370{-# INLINE_NORMAL intFromThenToDn #-}
371intFromThenToDn :: (Monad m, Integral a) => a -> a -> a -> Stream m a
372intFromThenToDn from next to = Stream step EnumInit
374 {-# INLINE_LATE step #-}
376 return $ if to > next
379 else Yield from EnumStop
380 else -- from >= next >= to
381 let stride = next - from
382 in Skip $ EnumYield from stride (to - stride)
384 step _ (EnumYield x stride toMinus) =
387 then Yield x EnumStop
388 else Yield x $ EnumYield (x + stride) stride toMinus
390 step _ EnumStop = return Stop
392{-# INLINE_NORMAL intFromThenTo #-}
393intFromThenTo :: (Monad m, Integral a) => a -> a -> a -> Stream m a
394intFromThenTo from next to
395 | next >= from = intFromThenToUp from next to
396 | otherwise = intFromThenToDn from next to
398{-# INLINE_NORMAL intFromThen #-}
399intFromThen :: (Monad m, Integral a, Bounded a) => a -> a -> Stream m a
400intFromThen from next =
402 then intFromThenToUp from next maxBound
403 else intFromThenToDn from next minBound
405-- For floating point numbers if the increment is less than the precision then
406-- it just gets lost. Therefore we cannot always increment it correctly by just
408-- 9007199254740992 + 1 + 1 :: Double => 9.007199254740992e15
409-- 9007199254740992 + 2 :: Double => 9.007199254740994e15
411-- Instead we accumulate the increment counter and compute the increment
412-- everytime before adding it to the starting number.
414-- This works for Integrals as well as floating point numbers, but intFromStep
415-- is faster for integrals.
416{-# INLINE_NORMAL numFromStep #-}
417numFromStep :: (Monad m, Num a) => a -> a -> Stream m a
418numFromStep from stride = Stream step 0
420 {-# INLINE_LATE step #-}
421 step _ !i = return $ (Yield $! (from + i * stride)) $! (i + 1)
423{-# INLINE_NORMAL numFrom #-}
424numFrom :: (Monad m, Num a) => a -> Stream m a
425numFrom from = numFromStep from 1
427{-# INLINE_NORMAL numFromThen #-}
428numFromThen :: (Monad m, Num a) => a -> a -> Stream m a
429numFromThen from next = numFromStep from (next - from)
431-- We cannot write a general function for Num. The only way to write code
432-- portable between the two is to use a 'Real' constraint and convert between
433-- Fractional and Integral using fromRational which is horribly slow.
434{-# INLINE_NORMAL fracFromTo #-}
435fracFromTo :: (Monad m, Fractional a, Ord a) => a -> a -> Stream m a
436fracFromTo from to = takeWhile (<= to + 1/2) $ numFromStep from 1
438{-# INLINE_NORMAL fracFromThenTo #-}
439fracFromThenTo :: (Monad m, Fractional a, Ord a) => a -> a -> a -> Stream m a
440fracFromThenTo from next to = takeWhile predicate $ numFromThen from next
442 mid = (next - from) / 2
443 predicate | next >= from = (<= to + mid)
444 | otherwise = (>= to + mid)
446-------------------------------------------------------------------------------
447-- Generation by Conversion
448-------------------------------------------------------------------------------
450-- | Create a singleton 'Stream' from a pure value.
451{-# INLINE_NORMAL yield #-}
452yield :: Monad m => a -> Stream m a
453yield x = Stream (\_ s -> return $ step undefined s) True
455 {-# INLINE_LATE step #-}
456 step _ True = Yield x False
459-- | Create a singleton 'Stream' from a monadic action.
460{-# INLINE_NORMAL yieldM #-}
461yieldM :: Monad m => m a -> Stream m a
462yieldM m = Stream step True
464 {-# INLINE_LATE step #-}
465 step _ True = m >>= \x -> return $ Yield x False
466 step _ False = return Stop
468{-# INLINE_NORMAL fromIndicesM #-}
469fromIndicesM :: Monad m => (Int -> m a) -> Stream m a
470fromIndicesM gen = Stream step 0
472 {-# INLINE_LATE step #-}
475 return $ Yield x (i + 1)
477{-# INLINE fromIndices #-}
478fromIndices :: Monad m => (Int -> a) -> Stream m a
479fromIndices gen = fromIndicesM (return . gen)
481{-# INLINE_NORMAL generateM #-}
482generateM :: Monad m => Int -> (Int -> m a) -> Stream m a
483generateM n gen = n `seq` Stream step 0
485 {-# INLINE_LATE step #-}
486 step _ i | i < n = do
488 return $ Yield x (i + 1)
489 | otherwise = return Stop
491{-# INLINE generate #-}
492generate :: Monad m => Int -> (Int -> a) -> Stream m a
493generate n gen = generateM n (return . gen)
495-- XXX we need the MonadAsync constraint because of a rewrite rule.
496-- | Convert a list of monadic actions to a 'Stream'
497{-# INLINE_LATE fromListM #-}
498fromListM :: MonadAsync m => [m a] -> Stream m a
499fromListM = Stream step
501 {-# INLINE_LATE step #-}
502 step _ (m:ms) = m >>= \x -> return $ Yield x ms
503 step _ [] = return Stop
505-- | Convert a list of pure values to a 'Stream'
506{-# INLINE_LATE fromList #-}
507fromList :: Monad m => [a] -> Stream m a
508fromList = Stream step
510 {-# INLINE_LATE step #-}
511 step _ (x:xs) = return $ Yield x xs
512 step _ [] = return Stop
514{-# INLINE_LATE fromStreamK #-}
515fromStreamK :: Monad m => K.Stream m a -> Stream m a
516fromStreamK = Stream step
519 let stop = return Stop
520 single a = return $ Yield a K.nil
521 yieldk a r = return $ Yield a r
522 in K.unStreamShared m1 gst stop single yieldk
524{-# INLINE toStreamD #-}
525toStreamD :: (K.IsStream t, Monad m) => t m a -> Stream m a
526toStreamD = fromStreamK . K.toStream
528------------------------------------------------------------------------------
529-- Elimination by Folds
530------------------------------------------------------------------------------
532{-# INLINE_NORMAL foldrM #-}
533foldrM :: Monad m => (a -> b -> m b) -> b -> Stream m a -> m b
534foldrM f z (Stream step state) = go SPEC state
537 r <- step defState st
539 Yield x s -> go SPEC s >>= f x
543{-# INLINE_NORMAL foldr #-}
544foldr :: Monad m => (a -> b -> b) -> b -> Stream m a -> m b
545foldr f = foldrM (\a b -> return (f a b))
547{-# INLINE_NORMAL foldr1 #-}
548foldr1 :: Monad m => (a -> a -> a) -> Stream m a -> m (Maybe a)
552 Nothing -> return Nothing
553 Just (h, t) -> fmap Just (foldr f h t)
555{-# INLINE_NORMAL foldlM' #-}
556foldlM' :: Monad m => (b -> a -> m b) -> b -> Stream m a -> m b
557foldlM' fstep begin (Stream step state) = go SPEC begin state
559 go !_ acc st = acc `seq` do
560 r <- step defState st
565 Skip s -> go SPEC acc s
569foldl' :: Monad m => (b -> a -> b) -> b -> Stream m a -> m b
570foldl' fstep = foldlM' (\b a -> return (fstep b a))
572------------------------------------------------------------------------------
574------------------------------------------------------------------------------
576-- | Run a streaming composition, discard the results.
577{-# INLINE_LATE runStream #-}
578runStream :: Monad m => Stream m a -> m ()
579runStream (Stream step state) = go SPEC state
582 r <- step defState st
584 Yield _ s -> go SPEC s
588{-# INLINE_NORMAL null #-}
589null :: Monad m => Stream m a -> m Bool
590null (Stream step state) = go state
593 r <- step defState st
595 Yield _ _ -> return False
600{-# INLINE_NORMAL head #-}
601head :: Monad m => Stream m a -> m (Maybe a)
602head (Stream step state) = go state
605 r <- step defState st
607 Yield x _ -> return (Just x)
609 Stop -> return Nothing
611-- Does not fuse, has the same performance as the StreamK version.
612{-# INLINE_NORMAL tail #-}
613tail :: Monad m => Stream m a -> m (Maybe (Stream m a))
614tail (Stream step state) = go state
617 r <- step defState st
619 Yield _ s -> return (Just $ Stream step s)
621 Stop -> return Nothing
623-- XXX will it fuse? need custom impl?
624{-# INLINE_NORMAL last #-}
625last :: Monad m => Stream m a -> m (Maybe a)
626last = foldl' (\_ y -> Just y) Nothing
628{-# INLINE_NORMAL elem #-}
629elem :: (Monad m, Eq a) => a -> Stream m a -> m Bool
630elem e (Stream step state) = go state
633 r <- step defState st
636 | x == e -> return True
641{-# INLINE_NORMAL notElem #-}
642notElem :: (Monad m, Eq a) => a -> Stream m a -> m Bool
643notElem e s = fmap not (elem e s)
645{-# INLINE_NORMAL all #-}
646all :: Monad m => (a -> Bool) -> Stream m a -> m Bool
647all p (Stream step state) = go state
650 r <- step defState st
654 | otherwise -> return False
658{-# INLINE_NORMAL any #-}
659any :: Monad m => (a -> Bool) -> Stream m a -> m Bool
660any p (Stream step state) = go state
663 r <- step defState st
671{-# INLINE_NORMAL maximum #-}
672maximum :: (Monad m, Ord a) => Stream m a -> m (Maybe a)
673maximum (Stream step state) = go Nothing state
676 r <- step defState st
678 Yield x s -> go (Just x) s
679 Skip s -> go Nothing s
680 Stop -> return Nothing
681 go (Just acc) st = do
682 r <- step defState st
685 | acc <= x -> go (Just x) s
686 | otherwise -> go (Just acc) s
687 Skip s -> go (Just acc) s
688 Stop -> return (Just acc)
690{-# INLINE_NORMAL maximumBy #-}
691maximumBy :: Monad m => (a -> a -> Ordering) -> Stream m a -> m (Maybe a)
692maximumBy cmp (Stream step state) = go Nothing state
695 r <- step defState st
697 Yield x s -> go (Just x) s
698 Skip s -> go Nothing s
699 Stop -> return Nothing
700 go (Just acc) st = do
701 r <- step defState st
703 Yield x s -> case cmp acc x of
704 GT -> go (Just acc) s
706 Skip s -> go (Just acc) s
707 Stop -> return (Just acc)
709{-# INLINE_NORMAL minimum #-}
710minimum :: (Monad m, Ord a) => Stream m a -> m (Maybe a)
711minimum (Stream step state) = go Nothing state
714 r <- step defState st
716 Yield x s -> go (Just x) s
717 Skip s -> go Nothing s
718 Stop -> return Nothing
719 go (Just acc) st = do
720 r <- step defState st
723 | acc <= x -> go (Just acc) s
724 | otherwise -> go (Just x) s
725 Skip s -> go (Just acc) s
726 Stop -> return (Just acc)
728{-# INLINE_NORMAL minimumBy #-}
729minimumBy :: Monad m => (a -> a -> Ordering) -> Stream m a -> m (Maybe a)
730minimumBy cmp (Stream step state) = go Nothing state
733 r <- step defState st
735 Yield x s -> go (Just x) s
736 Skip s -> go Nothing s
737 Stop -> return Nothing
738 go (Just acc) st = do
739 r <- step defState st
741 Yield x s -> case cmp acc x of
744 Skip s -> go (Just acc) s
745 Stop -> return (Just acc)
747{-# INLINE_NORMAL (!!) #-}
748(!!) :: (Monad m) => Stream m a -> Int -> m (Maybe a)
749(Stream step state) !! i = go i state
752 r <- step defState st
754 Yield x s | n < 0 -> return Nothing
755 | n == 0 -> return $ Just x
756 | otherwise -> go (n - 1) s
758 Stop -> return Nothing
760{-# INLINE_NORMAL lookup #-}
761lookup :: (Monad m, Eq a) => a -> Stream m (a, b) -> m (Maybe b)
762lookup e (Stream step state) = go state
765 r <- step defState st
767 Yield (a, b) s -> if e == a then return (Just b) else go s
769 Stop -> return Nothing
771{-# INLINE_NORMAL findM #-}
772findM :: Monad m => (a -> m Bool) -> Stream m a -> m (Maybe a)
773findM p (Stream step state) = go SPEC state
776 r <- step defState st
780 if b then return (Just x) else go SPEC s
782 Stop -> return Nothing
785find :: Monad m => (a -> Bool) -> Stream m a -> m (Maybe a)
786find p = findM (return . p)
788{-# INLINE_NORMAL findIndices #-}
789findIndices :: Monad m => (a -> Bool) -> Stream m a -> Stream m Int
790findIndices p (Stream step state) = Stream step' (state, 0)
792 {-# INLINE_LATE step' #-}
793 step' gst (st, i) = do
794 r <- step (adaptState gst) st
796 Yield x s -> if p x then Yield i (s, i+1) else Skip (s, i+1)
797 Skip s -> Skip (s, i+1)
800{-# INLINE_NORMAL concatMapM #-}
801concatMapM :: Monad m => (a -> m (Stream m b)) -> Stream m a -> Stream m b
802concatMapM f (Stream step state) = Stream step' (Left state)
804 {-# INLINE_LATE step' #-}
805 step' gst (Left st) = do
806 r <- step (adaptState gst) st
810 return $ Skip (Right (b_stream, s))
811 Skip s -> return $ Skip (Left s)
814 step' _ (Right (Stream inner_step inner_st, st)) = do
815 r <- inner_step defState inner_st
818 return $ Yield b (Right (Stream inner_step inner_s, st))
820 return $ Skip (Right (Stream inner_step inner_s, st))
821 Stop -> return $ Skip (Left st)
823{-# INLINE concatMap #-}
824concatMap :: Monad m => (a -> Stream m b) -> Stream m a -> Stream m b
825concatMap f = concatMapM (return . f)
827------------------------------------------------------------------------------
829------------------------------------------------------------------------------
831{-# INLINE_NORMAL isPrefixOf #-}
832isPrefixOf :: (Eq a, Monad m) => Stream m a -> Stream m a -> m Bool
833isPrefixOf (Stream stepa ta) (Stream stepb tb) = go (ta, tb, Nothing)
835 go (sa, sb, Nothing) = do
836 r <- stepa defState sa
838 Yield x sa' -> go (sa', sb, Just x)
839 Skip sa' -> go (sa', sb, Nothing)
842 go (sa, sb, Just x) = do
843 r <- stepb defState sb
847 then go (sa, sb', Nothing)
849 Skip sb' -> go (sa, sb', Just x)
852{-# INLINE_NORMAL isSubsequenceOf #-}
853isSubsequenceOf :: (Eq a, Monad m) => Stream m a -> Stream m a -> m Bool
854isSubsequenceOf (Stream stepa ta) (Stream stepb tb) = go (ta, tb, Nothing)
856 go (sa, sb, Nothing) = do
857 r <- stepa defState sa
859 Yield x sa' -> go (sa', sb, Just x)
860 Skip sa' -> go (sa', sb, Nothing)
863 go (sa, sb, Just x) = do
864 r <- stepb defState sb
868 then go (sa, sb', Nothing)
869 else go (sa, sb', Just x)
870 Skip sb' -> go (sa, sb', Just x)
873{-# INLINE_NORMAL stripPrefix #-}
876 => Stream m a -> Stream m a -> m (Maybe (Stream m a))
877stripPrefix (Stream stepa ta) (Stream stepb tb) = go (ta, tb, Nothing)
879 go (sa, sb, Nothing) = do
880 r <- stepa defState sa
882 Yield x sa' -> go (sa', sb, Just x)
883 Skip sa' -> go (sa', sb, Nothing)
884 Stop -> return $ Just (Stream stepb sb)
886 go (sa, sb, Just x) = do
887 r <- stepb defState sb
891 then go (sa, sb', Nothing)
893 Skip sb' -> go (sa, sb', Just x)
894 Stop -> return Nothing
896------------------------------------------------------------------------------
898------------------------------------------------------------------------------
900-- | Execute a monadic action for each element of the 'Stream'
901{-# INLINE_NORMAL mapM_ #-}
902mapM_ :: Monad m => (a -> m b) -> Stream m a -> m ()
903mapM_ m = runStream . mapM m
905------------------------------------------------------------------------------
907------------------------------------------------------------------------------
910toList :: Monad m => Stream m a -> m [a]
913-- Convert a direct stream to and from CPS encoded stream
914{-# INLINE_LATE toStreamK #-}
915toStreamK :: Monad m => Stream m a -> K.Stream m a
916toStreamK (Stream step state) = go state
918 go st = K.mkStream $ \gst stp sng yld -> do
921 Yield x s -> yld x (go s)
922 Skip s -> K.unStreamShared (go s) gst stp sng yld
925#ifndef DISABLE_FUSION
926{-# RULES "fromStreamK/toStreamK fusion"
927 forall s. toStreamK (fromStreamK s) = s #-}
928{-# RULES "toStreamK/fromStreamK fusion"
929 forall s. fromStreamK (toStreamK s) = s #-}
932{-# INLINE fromStreamD #-}
933fromStreamD :: (K.IsStream t, Monad m) => Stream m a -> t m a
934fromStreamD = K.fromStream . toStreamK
936------------------------------------------------------------------------------
937-- Transformation by Folding (Scans)
938------------------------------------------------------------------------------
940-- XXX Is a prescan useful, discarding the last step does not sound useful? I
941-- am not sure about the utility of this function, so this is implemented but
942-- not exposed. We can expose it if someone provides good reasons why this is
945-- XXX We have to execute the stream one step ahead to know that we are at the
946-- last step. The vector implementation of prescan executes the last fold step
947-- but does not yield the result. This means we have executed the effect but
948-- discarded value. This does not sound right. In this implementation we are
949-- not executing the last fold step.
950{-# INLINE_NORMAL prescanlM' #-}
951prescanlM' :: Monad m => (b -> a -> m b) -> m b -> Stream m a -> Stream m b
952prescanlM' f mz (Stream step state) = Stream step' (state, mz)
954 {-# INLINE_LATE step' #-}
955 step' gst (st, prev) = do
956 r <- step (adaptState gst) st
960 return $ Yield acc (s, f acc x)
961 Skip s -> return $ Skip (s, prev)
964{-# INLINE prescanl' #-}
965prescanl' :: Monad m => (b -> a -> b) -> b -> Stream m a -> Stream m b
966prescanl' f z = prescanlM' (\a b -> return (f a b)) (return z)
968-- XXX if we make the initial value of the accumulator monadic then should we
969-- execute it even if the stream is empty? In that case we would have generated
970-- the effect but discarded the value, but that is what a fold does when the
971-- stream is empty. Whatever we decide, need to reconcile this with prescan.
972-- If we execute the initial value here without even using it then it is ok to
973-- execute the last step there as well without using the value.
974-- Looking at the duality with right fold, in case of right fold we always
975-- perform the action when the construction terminates, so in case of left fold
976-- we should perform it only when the reduction starts.
977{-# INLINE_NORMAL postscanlM' #-}
978postscanlM' :: Monad m => (b -> a -> m b) -> b -> Stream m a -> Stream m b
979postscanlM' fstep begin (Stream step state) =
980 begin `seq` Stream step' (state, begin)
982 {-# INLINE_LATE step' #-}
983 step' gst (st, acc) = acc `seq` do
984 r <- step (adaptState gst) st
988 y `seq` return (Yield y (s, y))
989 Skip s -> return $ Skip (s, acc)
992{-# INLINE_NORMAL postscanl' #-}
993postscanl' :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
994postscanl' f = postscanlM' (\a b -> return (f a b))
996{-# INLINE_NORMAL postscanlM #-}
997postscanlM :: Monad m => (b -> a -> m b) -> b -> Stream m a -> Stream m b
998postscanlM fstep begin (Stream step state) = Stream step' (state, begin)
1000 {-# INLINE_LATE step' #-}
1001 step' gst (st, acc) = do
1002 r <- step (adaptState gst) st
1006 return (Yield y (s, y))
1007 Skip s -> return $ Skip (s, acc)
1010{-# INLINE_NORMAL postscanl #-}
1011postscanl :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
1012postscanl f = postscanlM (\a b -> return (f a b))
1014{-# INLINE_NORMAL scanlM' #-}
1015scanlM' :: Monad m => (b -> a -> m b) -> b -> Stream m a -> Stream m b
1016scanlM' fstep begin s = begin `seq` (begin `cons` postscanlM' fstep begin s)
1018{-# INLINE scanl' #-}
1019scanl' :: Monad m => (b -> a -> b) -> b -> Stream m a -> Stream m b
1020scanl' f = scanlM' (\a b -> return (f a b))
1022{-# INLINE_NORMAL scanlM #-}
1023scanlM :: Monad m => (b -> a -> m b) -> b -> Stream m a -> Stream m b
1024scanlM fstep begin s = begin `cons` postscanlM fstep begin s
1027scanl :: Monad m => (b -> a -> b) -> b -> Stream m a -> Stream m b
1028scanl f = scanlM (\a b -> return (f a b))
1030{-# INLINE_NORMAL scanl1M #-}
1031scanl1M :: Monad m => (a -> a -> m a) -> Stream m a -> Stream m a
1032scanl1M fstep (Stream step state) = Stream step' (state, Nothing)
1034 {-# INLINE_LATE step' #-}
1035 step' gst (st, Nothing) = do
1038 Yield x s -> return $ Yield x (s, Just x)
1039 Skip s -> return $ Skip (s, Nothing)
1042 step' gst (st, Just acc) = do
1047 return $ Yield z (s, Just z)
1048 Skip s -> return $ Skip (s, Just acc)
1051{-# INLINE scanl1 #-}
1052scanl1 :: Monad m => (a -> a -> a) -> Stream m a -> Stream m a
1053scanl1 f = scanl1M (\x y -> return (f x y))
1055{-# INLINE_NORMAL scanl1M' #-}
1056scanl1M' :: Monad m => (a -> a -> m a) -> Stream m a -> Stream m a
1057scanl1M' fstep (Stream step state) = Stream step' (state, Nothing)
1059 {-# INLINE_LATE step' #-}
1060 step' gst (st, Nothing) = do
1063 Yield x s -> x `seq` return $ Yield x (s, Just x)
1064 Skip s -> return $ Skip (s, Nothing)
1067 step' gst (st, Just acc) = acc `seq` do
1072 z `seq` return $ Yield z (s, Just z)
1073 Skip s -> return $ Skip (s, Just acc)
1076{-# INLINE scanl1' #-}
1077scanl1' :: Monad m => (a -> a -> a) -> Stream m a -> Stream m a
1078scanl1' f = scanl1M' (\x y -> return (f x y))
1080-------------------------------------------------------------------------------
1082-------------------------------------------------------------------------------
1084{-# INLINE_NORMAL take #-}
1085take :: Monad m => Int -> Stream m a -> Stream m a
1086take n (Stream step state) = n `seq` Stream step' (state, 0)
1088 {-# INLINE_LATE step' #-}
1089 step' gst (st, i) | i < n = do
1092 Yield x s -> Yield x (s, i + 1)
1093 Skip s -> Skip (s, i)
1095 step' _ (_, _) = return Stop
1097{-# INLINE_NORMAL takeWhileM #-}
1098takeWhileM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m a
1099takeWhileM f (Stream step state) = Stream step' state
1101 {-# INLINE_LATE step' #-}
1107 return $ if b then Yield x s else Stop
1108 Skip s -> return $ Skip s
1111{-# INLINE takeWhile #-}
1112takeWhile :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
1113takeWhile f = takeWhileM (return . f)
1115{-# INLINE_NORMAL drop #-}
1116drop :: Monad m => Int -> Stream m a -> Stream m a
1117drop n (Stream step state) = Stream step' (state, Just n)
1119 {-# INLINE_LATE step' #-}
1120 step' gst (st, Just i)
1125 Yield _ s -> Skip (s, Just (i - 1))
1126 Skip s -> Skip (s, Just i)
1128 | otherwise = return $ Skip (st, Nothing)
1130 step' gst (st, Nothing) = do
1134 Yield x s -> Yield x (s, Nothing)
1135 Skip s -> Skip (s, Nothing)
1138data DropWhileState s a
1140 | DropWhileYield a s
1143{-# INLINE_NORMAL dropWhileM #-}
1144dropWhileM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m a
1145dropWhileM f (Stream step state) = Stream step' (DropWhileDrop state)
1147 {-# INLINE_LATE step' #-}
1148 step' gst (DropWhileDrop st) = do
1154 then return $ Skip (DropWhileDrop s)
1155 else return $ Skip (DropWhileYield x s)
1156 Skip s -> return $ Skip (DropWhileDrop s)
1159 step' gst (DropWhileNext st) = do
1162 Yield x s -> return $ Skip (DropWhileYield x s)
1163 Skip s -> return $ Skip (DropWhileNext s)
1166 step' _ (DropWhileYield x st) = return $ Yield x (DropWhileNext st)
1168{-# INLINE dropWhile #-}
1169dropWhile :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
1170dropWhile f = dropWhileM (return . f)
1172{-# INLINE_NORMAL filterM #-}
1173filterM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m a
1174filterM f (Stream step state) = Stream step' state
1176 {-# INLINE_LATE step' #-}
1185 Skip s -> return $ Skip s
1188{-# INLINE filter #-}
1189filter :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
1190filter f = filterM (return . f)
1192{-# INLINE_NORMAL uniq #-}
1193uniq :: (Eq a, Monad m) => Stream m a -> Stream m a
1194uniq (Stream step state) = Stream step' (Nothing, state)
1196 {-# INLINE_LATE step' #-}
1197 step' gst (Nothing, st) = do
1200 Yield x s -> return $ Yield x (Just x, s)
1201 Skip s -> return $ Skip (Nothing, s)
1203 step' gst (Just x, st) = do
1206 Yield y s | x == y -> return $ Skip (Just x, s)
1207 | otherwise -> return $ Yield x (Just y, s)
1208 Skip s -> return $ Skip (Just x, s)
1211------------------------------------------------------------------------------
1212-- Transformation by Mapping
1213------------------------------------------------------------------------------
1215-- | Map a monadic function over a 'Stream'
1216{-# INLINE_NORMAL mapM #-}
1217mapM :: Monad m => (a -> m b) -> Stream m a -> Stream m b
1218mapM f (Stream step state) = Stream step' state
1220 {-# INLINE_LATE step' #-}
1222 r <- step (adaptState gst) st
1224 Yield x s -> f x >>= \a -> return $ Yield a s
1225 Skip s -> return $ Skip s
1229map :: Monad m => (a -> b) -> Stream m a -> Stream m b
1230map f = mapM (return . f)
1232{-# INLINE_NORMAL sequence #-}
1233sequence :: Monad m => Stream m (m a) -> Stream m a
1234sequence (Stream step state) = Stream step' state
1236 {-# INLINE_LATE step' #-}
1238 r <- step (adaptState gst) st
1240 Yield x s -> x >>= \a -> return (Yield a s)
1241 Skip s -> return $ Skip s
1244------------------------------------------------------------------------------
1246------------------------------------------------------------------------------
1248{-# INLINE_NORMAL insertBy #-}
1249insertBy :: Monad m => (a -> a -> Ordering) -> a -> Stream m a -> Stream m a
1250insertBy cmp a (Stream step state) = Stream step' (state, False, Nothing)
1252 {-# INLINE_LATE step' #-}
1253 step' gst (st, False, _) = do
1256 Yield x s -> case cmp a x of
1257 GT -> return $ Yield x (s, False, Nothing)
1258 _ -> return $ Yield a (s, True, Just x)
1259 Skip s -> return $ Skip (s, False, Nothing)
1260 Stop -> return $ Yield a (st, True, Nothing)
1262 step' _ (_, True, Nothing) = return Stop
1264 step' gst (st, True, Just prev) = do
1267 Yield x s -> return $ Yield prev (s, True, Just x)
1268 Skip s -> return $ Skip (s, True, Just prev)
1269 Stop -> return $ Yield prev (st, True, Nothing)
1271------------------------------------------------------------------------------
1273------------------------------------------------------------------------------
1275{-# INLINE_NORMAL deleteBy #-}
1276deleteBy :: Monad m => (a -> a -> Bool) -> a -> Stream m a -> Stream m a
1277deleteBy eq x (Stream step state) = Stream step' (state, False)
1279 {-# INLINE_LATE step' #-}
1280 step' gst (st, False) = do
1283 Yield y s -> return $
1284 if eq x y then Skip (s, True) else Yield y (s, False)
1285 Skip s -> return $ Skip (s, False)
1288 step' gst (st, True) = do
1291 Yield y s -> return $ Yield y (s, True)
1292 Skip s -> return $ Skip (s, True)
1295------------------------------------------------------------------------------
1296-- Transformation by Map and Filter
1297------------------------------------------------------------------------------
1299-- XXX Will this always fuse properly?
1300{-# INLINE_NORMAL mapMaybe #-}
1301mapMaybe :: Monad m => (a -> Maybe b) -> Stream m a -> Stream m b
1302mapMaybe f = fmap fromJust . filter isJust . map f
1304{-# INLINE_NORMAL mapMaybeM #-}
1305mapMaybeM :: Monad m => (a -> m (Maybe b)) -> Stream m a -> Stream m b
1306mapMaybeM f = fmap fromJust . filter isJust . mapM f
1308------------------------------------------------------------------------------
1310------------------------------------------------------------------------------
1312{-# INLINE_NORMAL indexed #-}
1313indexed :: Monad m => Stream m a -> Stream m (Int, a)
1314indexed (Stream step state) = Stream step' (state, 0)
1316 {-# INLINE_LATE step' #-}
1317 step' gst (st, i) = i `seq` do
1318 r <- step (adaptState gst) st
1320 Yield x s -> return $ Yield (i, x) (s, i+1)
1321 Skip s -> return $ Skip (s, i)
1324{-# INLINE_NORMAL indexedR #-}
1325indexedR :: Monad m => Int -> Stream m a -> Stream m (Int, a)
1326indexedR m (Stream step state) = Stream step' (state, m)
1328 {-# INLINE_LATE step' #-}
1329 step' gst (st, i) = i `seq` do
1330 r <- step (adaptState gst) st
1332 Yield x s -> let i' = i - 1
1334 return $ Yield (i', x) (s, i')
1335 Skip s -> return $ Skip (s, i)
1338{-# INLINE_NORMAL zipWithM #-}
1340 => (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
1341zipWithM f (Stream stepa ta) (Stream stepb tb) = Stream step (ta, tb, Nothing)
1343 {-# INLINE_LATE step #-}
1344 step gst (sa, sb, Nothing) = do
1345 r <- stepa (adaptState gst) sa
1348 Yield x sa' -> Skip (sa', sb, Just x)
1349 Skip sa' -> Skip (sa', sb, Nothing)
1352 step gst (sa, sb, Just x) = do
1353 r <- stepb (adaptState gst) sb
1357 return $ Yield z (sa, sb', Nothing)
1358 Skip sb' -> return $ Skip (sa, sb', Just x)
1361{-# RULES "zipWithM xs xs"
1362 forall f xs. zipWithM f xs xs = mapM (\x -> f x x) xs #-}
1364{-# INLINE zipWith #-}
1365zipWith :: Monad m => (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
1366zipWith f = zipWithM (\a b -> return (f a b))
1368------------------------------------------------------------------------------
1370------------------------------------------------------------------------------
1372{-# INLINE_NORMAL eqBy #-}
1373eqBy :: Monad m => (a -> b -> Bool) -> Stream m a -> Stream m b -> m Bool
1374eqBy eq (Stream step1 t1) (Stream step2 t2) = eq_loop0 SPEC t1 t2
1376 eq_loop0 !_ s1 s2 = do
1377 r <- step1 defState s1
1379 Yield x s1' -> eq_loop1 SPEC x s1' s2
1380 Skip s1' -> eq_loop0 SPEC s1' s2
1383 eq_loop1 !_ x s1 s2 = do
1384 r <- step2 defState s2
1387 | eq x y -> eq_loop0 SPEC s1 s2'
1388 | otherwise -> return False
1389 Skip s2' -> eq_loop1 SPEC x s1 s2'
1390 Stop -> return False
1393 r <- step2 defState s2
1395 Yield _ _ -> return False
1396 Skip s2' -> eq_null s2'
1399-- | Compare two streams lexicographically
1400{-# INLINE_NORMAL cmpBy #-}
1403 => (a -> b -> Ordering) -> Stream m a -> Stream m b -> m Ordering
1404cmpBy cmp (Stream step1 t1) (Stream step2 t2) = cmp_loop0 SPEC t1 t2
1406 cmp_loop0 !_ s1 s2 = do
1407 r <- step1 defState s1
1409 Yield x s1' -> cmp_loop1 SPEC x s1' s2
1410 Skip s1' -> cmp_loop0 SPEC s1' s2
1413 cmp_loop1 !_ x s1 s2 = do
1414 r <- step2 defState s2
1416 Yield y s2' -> case x `cmp` y of
1417 EQ -> cmp_loop0 SPEC s1 s2'
1419 Skip s2' -> cmp_loop1 SPEC x s1 s2'
1423 r <- step2 defState s2
1425 Yield _ _ -> return LT
1426 Skip s2' -> cmp_null s2'
1429------------------------------------------------------------------------------
1431------------------------------------------------------------------------------
1433{-# INLINE_NORMAL mergeByM #-}
1436 => (a -> a -> m Ordering) -> Stream m a -> Stream m a -> Stream m a
1437mergeByM cmp (Stream stepa ta) (Stream stepb tb) =
1438 Stream step (Just ta, Just tb, Nothing, Nothing)
1440 {-# INLINE_LATE step #-}
1442 -- one of the values is missing, and the corresponding stream is running
1443 step gst (Just sa, sb, Nothing, b) = do
1446 Yield a sa' -> Skip (Just sa', sb, Just a, b)
1447 Skip sa' -> Skip (Just sa', sb, Nothing, b)
1448 Stop -> Skip (Nothing, sb, Nothing, b)
1450 step gst (sa, Just sb, a, Nothing) = do
1453 Yield b sb' -> Skip (sa, Just sb', a, Just b)
1454 Skip sb' -> Skip (sa, Just sb', a, Nothing)
1455 Stop -> Skip (sa, Nothing, a, Nothing)
1457 -- both the values are available
1458 step _ (sa, sb, Just a, Just b) = do
1460 return $ case res of
1461 GT -> Yield b (sa, sb, Just a, Nothing)
1462 _ -> Yield a (sa, sb, Nothing, Just b)
1464 -- one of the values is missing, corresponding stream is done
1465 step _ (Nothing, sb, Nothing, Just b) =
1466 return $ Yield b (Nothing, sb, Nothing, Nothing)
1468 step _ (sa, Nothing, Just a, Nothing) =
1469 return $ Yield a (sa, Nothing, Nothing, Nothing)
1471 step _ (Nothing, Nothing, Nothing, Nothing) = return Stop
1473{-# INLINE mergeBy #-}
1476 => (a -> a -> Ordering) -> Stream m a -> Stream m a -> Stream m a
1477mergeBy cmp = mergeByM (\a b -> return $ cmp a b)
1479------------------------------------------------------------------------------
1480-- Transformation comprehensions
1481------------------------------------------------------------------------------
1483{-# INLINE_NORMAL the #-}
1484the :: (Eq a, Monad m) => Stream m a -> m (Maybe a)
1485the (Stream step state) = go state
1488 r <- step defState st
1490 Yield x s -> go' x s
1492 Stop -> return Nothing
1494 r <- step defState st
1496 Yield x s | x == n -> go' n s
1497 | otherwise -> return Nothing
1499 Stop -> return (Just n)
1501------------------------------------------------------------------------------
1503------------------------------------------------------------------------------
1505instance Monad m => Functor (Stream m) where
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list