@@ -1,10 +1,8 @@
1{-# LANGUAGE BangPatterns #-}
1{-# LANGUAGE BangPatterns #-}
2{-# LANGUAGE FlexibleContexts #-}
2{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE FlexibleInstances #-}
3{-# LANGUAGE FlexibleInstances #-}
4−{-# LANGUAGE GeneralizedNewtypeDeriving#-}
5{-# LANGUAGE MultiParamTypeClasses #-}
4{-# LANGUAGE MultiParamTypeClasses #-}
6{-# LANGUAGE RankNTypes #-}
5{-# LANGUAGE RankNTypes #-}
7−{-# LANGUAGE StandaloneDeriving #-}
8{-# LANGUAGE UndecidableInstances #-} -- XXX
6{-# LANGUAGE UndecidableInstances #-} -- XXX
@@ -73,7 +71,7 @@ module Streamly.Prelude
76−import Control.Monad (liftM)
74+import Control.Monad (void)
77import Control.Monad.IO.Class (MonadIO(..))
75import Control.Monad.IO.Class (MonadIO(..))
78import Data.Semigroup (Semigroup(..))
76import Data.Semigroup (Semigroup(..))
79import Prelude hiding (filter, drop, dropWhile, take,
77import Prelude hiding (filter, drop, dropWhile, take,
@@ -82,7 +80,7 @@ import Prelude hiding (filter, drop, dropWhile, take,
82 sum, product, elem, notElem,
80 sum, product, elem, notElem,
83 maximum, minimum, head, last,
81 maximum, minimum, head, last,
85−import qualified Prelude as Prelude
83+import qualified Prelude
86import qualified System.IO as IO
84import qualified System.IO as IO
88import Streamly.Core
86import Streamly.Core
@@ -96,7 +94,7 @@ import Streamly.Streams
96unfoldr :: Streaming t => (b -> Maybe (a, b)) -> b -> t m a
94unfoldr :: Streaming t => (b -> Maybe (a, b)) -> b -> t m a
97unfoldr step = fromStream . go
95unfoldr step = fromStream . go
99− go s = Stream $ \_ stp yld -> do
97+ go s = Stream $ \_ stp yld ->
100 case step s of
98 case step s of
101 Nothing -> stp
99 Nothing -> stp
102 Just (a, b) -> yld a (Just (go b))
100 Just (a, b) -> yld a (Just (go b))
@@ -115,11 +113,11 @@ unfoldrM step = fromStream . go
115-- | Same as @foldWith (<>)@ but more efficient.
113-- | Same as @foldWith (<>)@ but more efficient.
116{-# INLINE each #-}
114{-# INLINE each #-}
117each :: (Streaming t, Foldable f) => f a -> t m a
115each :: (Streaming t, Foldable f) => f a -> t m a
118−each xs = Prelude.foldr cons nil xs
116+each = Prelude.foldr cons nil
120-- | Read lines from an IO Handle into a stream of Strings.
118-- | Read lines from an IO Handle into a stream of Strings.
121fromHandle :: (Streaming t, MonadIO m) => IO.Handle -> t m String
119fromHandle :: (Streaming t, MonadIO m) => IO.Handle -> t m String
122−fromHandle h = fromStream $ go
120+fromHandle h = fromStream go
124 go = Stream $ \_ stp yld -> do
122 go = Stream $ \_ stp yld -> do
125 eof <- liftIO $ IO.hIsEOF h
123 eof <- liftIO $ IO.hIsEOF h
@@ -187,7 +185,7 @@ uncons :: (Streaming t, Monad m) => t m a -> m (Maybe (a, t m a))
187uncons m =
185uncons m =
188 let stop = return Nothing
186 let stop = return Nothing
189 yield a Nothing = return (Just (a, nil))
187 yield a Nothing = return (Just (a, nil))
190− yield a (Just x) = return (Just (a, (fromStream x)))
188+ yield a (Just x) = return (Just (a, fromStream x))
191 in (runStream (toStream m)) Nothing stop yield
189 in (runStream (toStream m)) Nothing stop yield
193-- | Write a stream of Strings to an IO Handle.
191-- | Write a stream of Strings to an IO Handle.
@@ -207,18 +205,16 @@ toHandle h m = go (toStream m)
207-- | Convert a stream into a list in the underlying monad.
205-- | Convert a stream into a list in the underlying monad.
208{-# INLINABLE toList #-}
206{-# INLINABLE toList #-}
209toList :: (Streaming t, Monad m) => t m a -> m [a]
207toList :: (Streaming t, Monad m) => t m a -> m [a]
210−toList = foldrM (\a xs -> liftM (a :) xs) (return [])
208+toList = foldrM (\a xs -> fmap (a :) xs) (return [])
212-- | Take first 'n' elements from the stream and discard the rest.
210-- | Take first 'n' elements from the stream and discard the rest.
213take :: Streaming t => Int -> t m a -> t m a
211take :: Streaming t => Int -> t m a -> t m a
214take n m = fromStream $ go n (toStream m)
212take n m = fromStream $ go n (toStream m)
216− go n1 m1 = Stream $ \ctx stp yld -> do
214+ go n1 m1 = Stream $ \ctx stp yld ->
217 let yield a Nothing = yld a Nothing
215 let yield a Nothing = yld a Nothing
218 yield a (Just x) = yld a (Just (go (n1 - 1) x))
216 yield a (Just x) = yld a (Just (go (n1 - 1) x))
221− else (runStream m1) ctx stp yield
217+ in if n1 <= 0 then stp else (runStream m1) ctx stp yield
223-- XXX This is not as efficient as it could be. We need a short circuiting at
219-- XXX This is not as efficient as it could be. We need a short circuiting at
224-- a lower level. Compare with simple-conduit, filtering there cuts down time
220-- a lower level. Compare with simple-conduit, filtering there cuts down time
@@ -229,7 +225,7 @@ take n m = fromStream $ go n (toStream m)
229filter :: Streaming t => (a -> Bool) -> t m a -> t m a
225filter :: Streaming t => (a -> Bool) -> t m a -> t m a
230filter p m = fromStream $ go (toStream m)
226filter p m = fromStream $ go (toStream m)
232− go m1 = Stream $ \ctx stp yld -> do
228+ go m1 = Stream $ \ctx stp yld ->
233 let yield a Nothing | p a = yld a Nothing
229 let yield a Nothing | p a = yld a Nothing
234 | otherwise = stp
230 | otherwise = stp
235 yield a (Just x) | p a = yld a (Just (go x))
231 yield a (Just x) | p a = yld a (Just (go x))
@@ -240,7 +236,7 @@ filter p m = fromStream $ go (toStream m)
240takeWhile :: Streaming t => (a -> Bool) -> t m a -> t m a
236takeWhile :: Streaming t => (a -> Bool) -> t m a -> t m a
241takeWhile p m = fromStream $ go (toStream m)
237takeWhile p m = fromStream $ go (toStream m)
243− go m1 = Stream $ \ctx stp yld -> do
239+ go m1 = Stream $ \ctx stp yld ->
244 let yield a Nothing | p a = yld a Nothing
240 let yield a Nothing | p a = yld a Nothing
245 | otherwise = stp
241 | otherwise = stp
246 yield a (Just x) | p a = yld a (Just (go x))
242 yield a (Just x) | p a = yld a (Just (go x))
@@ -251,19 +247,19 @@ takeWhile p m = fromStream $ go (toStream m)
251drop :: Streaming t => Int -> t m a -> t m a
247drop :: Streaming t => Int -> t m a -> t m a
252drop n m = fromStream $ go n (toStream m)
248drop n m = fromStream $ go n (toStream m)
254− go n1 m1 = Stream $ \ctx stp yld -> do
250+ go n1 m1 = Stream $ \ctx stp yld ->
255 let yield _ Nothing = stp
251 let yield _ Nothing = stp
256 yield _ (Just x) = (runStream $ go (n1 - 1) x) ctx stp yld
252 yield _ (Just x) = (runStream $ go (n1 - 1) x) ctx stp yld
257− if (n1 <= 0)
253+ in if n1 <= 0
258− then (runStream m1) ctx stp yld
254+ then (runStream m1) ctx stp yld
259− else (runStream m1) ctx stp yield
255+ else (runStream m1) ctx stp yield
261-- | Drop elements in the stream as long as the predicate succeeds and then
257-- | Drop elements in the stream as long as the predicate succeeds and then
262-- take the rest of the stream.
258-- take the rest of the stream.
263dropWhile :: Streaming t => (a -> Bool) -> t m a -> t m a
259dropWhile :: Streaming t => (a -> Bool) -> t m a -> t m a
264dropWhile p m = fromStream $ go (toStream m)
260dropWhile p m = fromStream $ go (toStream m)
266− go m1 = Stream $ \ctx stp yld -> do
262+ go m1 = Stream $ \ctx stp yld ->
267 let yield a Nothing | p a = stp
263 let yield a Nothing | p a = stp
268 | otherwise = yld a Nothing
264 | otherwise = yld a Nothing
269 yield a (Just x) | p a = (runStream (go x)) ctx stp yield
265 yield a (Just x) | p a = (runStream (go x)) ctx stp yield
@@ -324,7 +320,7 @@ elem e m = go (toStream m)
325 let stop = return False
321 let stop = return False
326 yield a Nothing = return (a == e)
322 yield a Nothing = return (a == e)
327− yield a (Just x) = if (a == e) then return True else go x
323+ yield a (Just x) = if a == e then return True else go x
328 in (runStream m1) Nothing stop yield
324 in (runStream m1) Nothing stop yield
330-- | Determine whether an element is not present in the stream.
326-- | Determine whether an element is not present in the stream.
@@ -334,7 +330,7 @@ notElem e m = go (toStream m)
335 let stop = return True
331 let stop = return True
336 yield a Nothing = return (a /= e)
332 yield a Nothing = return (a /= e)
337− yield a (Just x) = if (a == e) then return False else go x
333+ yield a (Just x) = if a == e then return False else go x
338 in (runStream m1) Nothing stop yield
334 in (runStream m1) Nothing stop yield
340-- | Determine the length of the stream.
336-- | Determine the length of the stream.
@@ -380,7 +376,7 @@ maximum m = go Nothing (toStream m)
380mapM :: (Streaming t, Monad m) => (a -> m b) -> t m a -> t m b
376mapM :: (Streaming t, Monad m) => (a -> m b) -> t m a -> t m b
381mapM f m = fromStream $ go (toStream m)
377mapM f m = fromStream $ go (toStream m)
383− go m1 = Stream $ \_ stp yld -> do
379+ go m1 = Stream $ \_ stp yld ->
384 let stop = stp
380 let stop = stp
385 yield a Nothing = f a >>= \b -> yld b Nothing
381 yield a Nothing = f a >>= \b -> yld b Nothing
386 yield a (Just x) = f a >>= \b -> yld b (Just (go x))
382 yield a (Just x) = f a >>= \b -> yld b (Just (go x))
@@ -393,7 +389,7 @@ mapM_ f m = go (toStream m)
395 let stop = return ()
391 let stop = return ()
396− yield a Nothing = f a >> return ()
392+ yield a Nothing = void (f a)
397 yield a (Just x) = f a >> go x
393 yield a (Just x) = f a >> go x
398 in (runStream m1) Nothing stop yield
394 in (runStream m1) Nothing stop yield
@@ -402,7 +398,7 @@ mapM_ f m = go (toStream m)
402sequence :: (Streaming t, Monad m) => t m (m a) -> t m a
398sequence :: (Streaming t, Monad m) => t m (m a) -> t m a
403sequence m = fromStream $ go (toStream m)
399sequence m = fromStream $ go (toStream m)
405− go m1 = Stream $ \_ stp yld -> do
401+ go m1 = Stream $ \_ stp yld ->
406 let stop = stp
402 let stop = stp
407 yield a Nothing = a >>= \b -> yld b Nothing
403 yield a Nothing = a >>= \b -> yld b Nothing
408 yield a (Just x) = a >>= \b -> yld b (Just (go x))
404 yield a (Just x) = a >>= \b -> yld b (Just (go x))
@@ -420,7 +416,7 @@ zipWithM f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
420 let merge a ra =
416 let merge a ra =
421 let yield2 b Nothing = (runStream (g a b)) Nothing stp yld
417 let yield2 b Nothing = (runStream (g a b)) Nothing stp yld
422 yield2 b (Just rb) =
418 yield2 b (Just rb) =
423− (runStream ((g a b) <> (go ra rb))) Nothing stp yld
419+ (runStream (g a b <> go ra rb)) Nothing stp yld
424 in (runStream my) Nothing stp yield2
420 in (runStream my) Nothing stp yield2
425 let yield1 a Nothing = merge a snil
421 let yield1 a Nothing = merge a snil
426 yield1 a (Just ra) = merge a ra
422 yield1 a (Just ra) = merge a ra