on master help sign in
Fix some hlint suggestions
3876222Harendra Kumar
src/Streamly/Core.hs+17−23
@@ -1,6 +1,7 @@
1{-# LANGUAGE ConstraintKinds #-}
1{-# LANGUAGE ConstraintKinds #-}
2{-# LANGUAGE FlexibleContexts #-}
2{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE FlexibleInstances #-}
3{-# LANGUAGE FlexibleInstances #-}
4+{-# LANGUAGE LambdaCase #-}
4{-# LANGUAGE MultiParamTypeClasses #-}
5{-# LANGUAGE MultiParamTypeClasses #-}
5{-# LANGUAGE RankNTypes #-}
6{-# LANGUAGE RankNTypes #-}
6{-# LANGUAGE UndecidableInstances #-} -- XXX
7{-# LANGUAGE UndecidableInstances #-} -- XXX
@@ -253,10 +254,9 @@ runqueueLIFO sv q = run
253 yield a Nothing = sendit a >> run
254 yield a Nothing = sendit a >> run
254 yield a (Just r) = sendit a >> (runStream r) (Just sv) run yield
255 yield a (Just r) = sendit a >> (runStream r) (Just sv) run yield
255
256
256− dequeue = liftIO $ atomicModifyIORefCAS q $ \ ms ->
257+ dequeue = liftIO $ atomicModifyIORefCAS q $ \case
257− case ms of
258− [] -> ([], Nothing)
258+ [] -> ([], Nothing)
259− x : xs -> (xs, Just x)
259+ x : xs -> (xs, Just x)
260
260
261{-# INLINE enqueueFIFO #-}
261{-# INLINE enqueueFIFO #-}
262enqueueFIFO :: LinkedQueue (Stream m a) -> Stream m a -> IO ()
262enqueueFIFO :: LinkedQueue (Stream m a) -> Stream m a -> IO ()
@@ -289,7 +289,7 @@ runqueueFIFO sv q = run
289{-# NOINLINE addThread #-}
289{-# NOINLINE addThread #-}
290addThread :: MonadIO m => SVar m a -> ThreadId -> m ()
290addThread :: MonadIO m => SVar m a -> ThreadId -> m ()
291addThread sv tid =
291addThread sv tid =
292− liftIO $ modifyIORef (runningThreads sv) $ (\s -> S.insert tid s)
292+ liftIO $ modifyIORef (runningThreads sv) (S.insert tid)
293
293
294{-# INLINE delThread #-}
294{-# INLINE delThread #-}
295delThread :: MonadIO m => SVar m a -> ThreadId -> m ()
295delThread :: MonadIO m => SVar m a -> ThreadId -> m ()
@@ -298,8 +298,7 @@ delThread sv tid =
298
298
299{-# INLINE allThreadsDone #-}
299{-# INLINE allThreadsDone #-}
300allThreadsDone :: MonadIO m => SVar m a -> m Bool
300allThreadsDone :: MonadIO m => SVar m a -> m Bool
301−allThreadsDone sv = liftIO $ do
301+allThreadsDone sv = liftIO $ S.null <$> readIORef (runningThreads sv)
302− readIORef (runningThreads sv) >>= return . S.null
303
302
304{-# NOINLINE handleChildException #-}
303{-# NOINLINE handleChildException #-}
305handleChildException :: MonadIO m => SVar m a -> SomeException -> m ()
304handleChildException :: MonadIO m => SVar m a -> SomeException -> m ()
@@ -326,8 +325,8 @@ sendWorkerWait sv = do
326 output <- liftIO $ readIORef (outputQueue sv)
325 output <- liftIO $ readIORef (outputQueue sv)
327 when (null output) $ do
326 when (null output) $ do
328 done <- queueEmpty sv
327 done <- queueEmpty sv
329− if (not done)
328+ if not done
330− then (pushWorker sv) >> sendWorkerWait sv
329+ then pushWorker sv >> sendWorkerWait sv
331 else void (liftIO $ takeMVar (doorBell sv))
330 else void (liftIO $ takeMVar (doorBell sv))
332
331
333-- | Pull a stream from an SVar.
332-- | Pull a stream from an SVar.
@@ -381,13 +380,13 @@ getFifoSVar ctype = do
381 running <- newIORef S.empty
380 running <- newIORef S.empty
382 q <- newQ
381 q <- newQ
383 let sv =
382 let sv =
384− SVar { outputQueue = outQ
383+ SVar { outputQueue = outQ
385 , doorBell = outQMv
384 , doorBell = outQMv
386 , runningThreads = running
385 , runningThreads = running
387 , runqueue = runqueueFIFO sv q
386 , runqueue = runqueueFIFO sv q
388 , enqueue = pushL q
387 , enqueue = pushL q
389 , queueEmpty = liftIO $ nullQ q
388 , queueEmpty = liftIO $ nullQ q
390− , svarStyle = ctype
389+ , svarStyle = ctype
391 }
390 }
392 in return sv
391 in return sv
393
392
@@ -397,30 +396,25 @@ getLifoSVar ctype = do
397 outQMv <- newEmptyMVar
396 outQMv <- newEmptyMVar
398 running <- newIORef S.empty
397 running <- newIORef S.empty
399 q <- newIORef []
398 q <- newIORef []
400− let checkEmpty = liftIO (readIORef q) >>= return . null
399+ let checkEmpty = null <$> liftIO (readIORef q)
401 let sv =
400 let sv =
402− SVar { outputQueue = outQ
401+ SVar { outputQueue = outQ
403 , doorBell = outQMv
402 , doorBell = outQMv
404 , runningThreads = running
403 , runningThreads = running
405 , runqueue = runqueueLIFO sv q
404 , runqueue = runqueueLIFO sv q
406 , enqueue = enqueueLIFO q
405 , enqueue = enqueueLIFO q
407 , queueEmpty = checkEmpty
406 , queueEmpty = checkEmpty
408− , svarStyle = ctype
407+ , svarStyle = ctype
409 }
408 }
410 in return sv
409 in return sv
411
410
412-- | Create a new empty SVar.
411-- | Create a new empty SVar.
413newEmptySVar :: MonadAsync m => SVarStyle -> m (SVar m a)
412newEmptySVar :: MonadAsync m => SVarStyle -> m (SVar m a)
414newEmptySVar style = do
413newEmptySVar style = do
415− sv <- liftIO $
414+ liftIO $
416 case style of
415 case style of
417− SVarStyle _ FIFO -> do
418− c <- getFifoSVar style
416+ SVarStyle _ FIFO -> getFifoSVar style
419− return c
420− SVarStyle _ LIFO -> do
421− c <- getLifoSVar style
417+ SVarStyle _ LIFO -> getLifoSVar style
422− return c
423− return sv
424
418
425-- | Create a new SVar and enqueue one stream computation on it.
419-- | Create a new SVar and enqueue one stream computation on it.
426newStreamVar1 :: MonadAsync m => SVarStyle -> Stream m a -> m (SVar m a)
420newStreamVar1 :: MonadAsync m => SVarStyle -> Stream m a -> m (SVar m a)
@@ -534,7 +528,7 @@ withNewSVar2 style m1 m2 = Stream $ \_ stp yld -> do
534{-# INLINE joinStreamVar2 #-}
528{-# INLINE joinStreamVar2 #-}
535joinStreamVar2 :: MonadAsync m
529joinStreamVar2 :: MonadAsync m
536 => SVarStyle -> Stream m a -> Stream m a -> Stream m a
530 => SVarStyle -> Stream m a -> Stream m a -> Stream m a
537−joinStreamVar2 style m1 m2 = Stream $ \st stp yld -> do
531+joinStreamVar2 style m1 m2 = Stream $ \st stp yld ->
538 case st of
532 case st of
539 Just sv | svarStyle sv == style ->
533 Just sv | svarStyle sv == style ->
540 liftIO ((enqueue sv) m2) >> (runStream m1) st stp yld
534 liftIO ((enqueue sv) m2) >> (runStream m1) st stp yld
src/Streamly/Prelude.hs+22−26
@@ -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
9
7
10-- |
8-- |
@@ -73,7 +71,7 @@ module Streamly.Prelude
73 )
71 )
74where
72where
75
73
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,
84 length)
82 length)
85−import qualified Prelude as Prelude
83+import qualified Prelude
86import qualified System.IO as IO
84import qualified System.IO as IO
87
85
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
98 where
96 where
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
119
117
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
123 where
121 where
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
192
190
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 [])
211
209
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)
215 where
213 where
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))
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
222
218
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)
231 where
227 where
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)
242 where
238 where
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)
253 where
249 where
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
260
256
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)
265 where
261 where
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)
324 go m1 =
320 go m1 =
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
329
325
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)
334 go m1 =
330 go m1 =
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
339
335
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)
382 where
378 where
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)
393 where
389 where
394 go m1 =
390 go m1 =
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
399
395
@@ -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)
404 where
400 where
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
src/Streamly/Streams.hs+3−3
@@ -94,7 +94,7 @@ import Control.Monad.Reader.Class (MonadReader(..))
94import Control.Monad.State.Class (MonadState(..))
94import Control.Monad.State.Class (MonadState(..))
95import Control.Monad.Trans.Class (MonadTrans)
95import Control.Monad.Trans.Class (MonadTrans)
96import Data.Semigroup (Semigroup(..))
96import Data.Semigroup (Semigroup(..))
97−import Prelude hiding (drop, take, zipWith)
97+import Prelude hiding (zipWith)
98import Streamly.Core
98import Streamly.Core
99
99
100------------------------------------------------------------------------------
100------------------------------------------------------------------------------
@@ -117,7 +117,7 @@ cons a r = fromStream $ scons a (Just (toStream r))
117
117
118-- | An empty stream.
118-- | An empty stream.
119nil :: Streaming t => t m a
119nil :: Streaming t => t m a
120−nil = fromStream $ snil
120+nil = fromStream snil
121
121
122-- | Build a stream from its church encoding. The function passed maps
122-- | Build a stream from its church encoding. The function passed maps
123-- directly to the underlying representation of the stream type. The second
123-- directly to the underlying representation of the stream type. The second
@@ -483,7 +483,7 @@ parbind par m f = go m
483 Stream $ \ctx stp yld ->
483 Stream $ \ctx stp yld ->
484 let run x = (runStream x) ctx stp yld
484 let run x = (runStream x) ctx stp yld
485 yield a Nothing = run $ f a
485 yield a Nothing = run $ f a
486− yield a (Just r) = run $ f a `par` (go r)
486+ yield a (Just r) = run $ f a `par` go r
487 in g Nothing stp yield
487 in g Nothing stp yield
488
488
489instance MonadAsync m => Monad (AsyncT m) where
489instance MonadAsync m => Monad (AsyncT m) where
src/Streamly/Time.hs+1−1
@@ -49,7 +49,7 @@ withClock clock freq action = do
49 action localTime
49 action localTime
50 when (delay > 0) $ threadDelay delay
50 when (delay > 0) $ threadDelay delay
51
51
52− if (n == freq)
52+ if n == freq
53 then do
53 then do
54 (t, newTick, newDelay) <- adjustClock lastAdj localTime delay
54 (t, newTick, newDelay) <- adjustClock lastAdj localTime delay
55 go t newDelay newTick (localTime + newTick) 0
55 go t newDelay newTick (localTime + newTick) 0