@@ -1,10 +1,8 @@
11{-# LANGUAGE BangPatterns #-}
22{-# LANGUAGE FlexibleContexts #-}
33{-# LANGUAGE FlexibleInstances #-}
4−{-# LANGUAGE GeneralizedNewtypeDeriving#-}
54{-# LANGUAGE MultiParamTypeClasses #-}
65{-# LANGUAGE RankNTypes #-}
7−{-# LANGUAGE StandaloneDeriving #-}
86{-# LANGUAGE UndecidableInstances #-} -- XXX
97
108-- |
@@ -73,7 +71,7 @@ module Streamly.Prelude
7371 )
7472where
7573
76−import Control.Monad (liftM)
74+import Control.Monad (void)
7775import Control.Monad.IO.Class (MonadIO(..))
7876import Data.Semigroup (Semigroup(..))
7977import Prelude hiding (filter, drop, dropWhile, take,
@@ -82,7 +80,7 @@ import Prelude hiding (filter, drop, dropWhile, take,
8280 sum, product, elem, notElem,
8381 maximum, minimum, head, last,
8482 length)
85−import qualified Prelude as Prelude
83+import qualified Prelude
8684import qualified System.IO as IO
8785
8886import Streamly.Core
@@ -96,7 +94,7 @@ import Streamly.Streams
9694unfoldr :: Streaming t => (b -> Maybe (a, b)) -> b -> t m a
9795unfoldr step = fromStream . go
9896 where
99− go s = Stream $ \_ stp yld -> do
97+ go s = Stream $ \_ stp yld ->
10098 case step s of
10199 Nothing -> stp
102100 Just (a, b) -> yld a (Just (go b))
@@ -115,11 +113,11 @@ unfoldrM step = fromStream . go
115113-- | Same as @foldWith (<>)@ but more efficient.
116114{-# INLINE each #-}
117115each :: (Streaming t, Foldable f) => f a -> t m a
118−each xs = Prelude.foldr cons nil xs
116+each = Prelude.foldr cons nil
119117
120118-- | Read lines from an IO Handle into a stream of Strings.
121119fromHandle :: (Streaming t, MonadIO m) => IO.Handle -> t m String
122−fromHandle h = fromStream $ go
120+fromHandle h = fromStream go
123121 where
124122 go = Stream $ \_ stp yld -> do
125123 eof <- liftIO $ IO.hIsEOF h
@@ -187,7 +185,7 @@ uncons :: (Streaming t, Monad m) => t m a -> m (Maybe (a, t m a))
187185uncons m =
188186 let stop = return Nothing
189187 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))
191189 in (runStream (toStream m)) Nothing stop yield
192190
193191-- | Write a stream of Strings to an IO Handle.
@@ -207,18 +205,16 @@ toHandle h m = go (toStream m)
207205-- | Convert a stream into a list in the underlying monad.
208206{-# INLINABLE toList #-}
209207toList :: (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 [])
211209
212210-- | Take first 'n' elements from the stream and discard the rest.
213211take :: Streaming t => Int -> t m a -> t m a
214212take n m = fromStream $ go n (toStream m)
215213 where
216− go n1 m1 = Stream $ \ctx stp yld -> do
214+ go n1 m1 = Stream $ \ctx stp yld ->
217215 let yield a Nothing = yld a Nothing
218216 yield a (Just x) = yld a (Just (go (n1 - 1) x))
219− if (n1 <= 0)
220− then stp
221− else (runStream m1) ctx stp yield
217+ in if n1 <= 0 then stp else (runStream m1) ctx stp yield
222218
223219-- XXX This is not as efficient as it could be. We need a short circuiting at
224220-- a lower level. Compare with simple-conduit, filtering there cuts down time
@@ -229,7 +225,7 @@ take n m = fromStream $ go n (toStream m)
229225filter :: Streaming t => (a -> Bool) -> t m a -> t m a
230226filter p m = fromStream $ go (toStream m)
231227 where
232− go m1 = Stream $ \ctx stp yld -> do
228+ go m1 = Stream $ \ctx stp yld ->
233229 let yield a Nothing | p a = yld a Nothing
234230 | otherwise = stp
235231 yield a (Just x) | p a = yld a (Just (go x))
@@ -240,7 +236,7 @@ filter p m = fromStream $ go (toStream m)
240236takeWhile :: Streaming t => (a -> Bool) -> t m a -> t m a
241237takeWhile p m = fromStream $ go (toStream m)
242238 where
243− go m1 = Stream $ \ctx stp yld -> do
239+ go m1 = Stream $ \ctx stp yld ->
244240 let yield a Nothing | p a = yld a Nothing
245241 | otherwise = stp
246242 yield a (Just x) | p a = yld a (Just (go x))
@@ -251,19 +247,19 @@ takeWhile p m = fromStream $ go (toStream m)
251247drop :: Streaming t => Int -> t m a -> t m a
252248drop n m = fromStream $ go n (toStream m)
253249 where
254− go n1 m1 = Stream $ \ctx stp yld -> do
250+ go n1 m1 = Stream $ \ctx stp yld ->
255251 let yield _ Nothing = stp
256252 yield _ (Just x) = (runStream $ go (n1 - 1) x) ctx stp yld
257− if (n1 <= 0)
258− then (runStream m1) ctx stp yld
259− else (runStream m1) ctx stp yield
253+ in if n1 <= 0
254+ then (runStream m1) ctx stp yld
255+ else (runStream m1) ctx stp yield
260256
261257-- | Drop elements in the stream as long as the predicate succeeds and then
262258-- take the rest of the stream.
263259dropWhile :: Streaming t => (a -> Bool) -> t m a -> t m a
264260dropWhile p m = fromStream $ go (toStream m)
265261 where
266− go m1 = Stream $ \ctx stp yld -> do
262+ go m1 = Stream $ \ctx stp yld ->
267263 let yield a Nothing | p a = stp
268264 | otherwise = yld a Nothing
269265 yield a (Just x) | p a = (runStream (go x)) ctx stp yield
@@ -324,7 +320,7 @@ elem e m = go (toStream m)
324320 go m1 =
325321 let stop = return False
326322 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
328324 in (runStream m1) Nothing stop yield
329325
330326-- | Determine whether an element is not present in the stream.
@@ -334,7 +330,7 @@ notElem e m = go (toStream m)
334330 go m1 =
335331 let stop = return True
336332 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
338334 in (runStream m1) Nothing stop yield
339335
340336-- | Determine the length of the stream.
@@ -380,7 +376,7 @@ maximum m = go Nothing (toStream m)
380376mapM :: (Streaming t, Monad m) => (a -> m b) -> t m a -> t m b
381377mapM f m = fromStream $ go (toStream m)
382378 where
383− go m1 = Stream $ \_ stp yld -> do
379+ go m1 = Stream $ \_ stp yld ->
384380 let stop = stp
385381 yield a Nothing = f a >>= \b -> yld b Nothing
386382 yield a (Just x) = f a >>= \b -> yld b (Just (go x))
@@ -393,7 +389,7 @@ mapM_ f m = go (toStream m)
393389 where
394390 go m1 =
395391 let stop = return ()
396− yield a Nothing = f a >> return ()
392+ yield a Nothing = void (f a)
397393 yield a (Just x) = f a >> go x
398394 in (runStream m1) Nothing stop yield
399395
@@ -402,7 +398,7 @@ mapM_ f m = go (toStream m)
402398sequence :: (Streaming t, Monad m) => t m (m a) -> t m a
403399sequence m = fromStream $ go (toStream m)
404400 where
405− go m1 = Stream $ \_ stp yld -> do
401+ go m1 = Stream $ \_ stp yld ->
406402 let stop = stp
407403 yield a Nothing = a >>= \b -> yld b Nothing
408404 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)
420416 let merge a ra =
421417 let yield2 b Nothing = (runStream (g a b)) Nothing stp yld
422418 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
424420 in (runStream my) Nothing stp yield2
425421 let yield1 a Nothing = merge a snil
426422 yield1 a (Just ra) = merge a ra