on master help sign in
Fix some hlint suggestions
commit3876222d22cb6d745f6764f278a5940e66b142fd authorHarendra Kumar <harendra.kumar@gmail.com> dateFri 9 Mar 2018, 12:51 (9 years ago) parent1f42a8d
4 files changed+43−53
src/Streamly/Core.hs+17−23 src/Streamly/Prelude.hs+22−26 src/Streamly/Streams.hs+3−3 src/Streamly/Time.hs+1−1
src/Streamly/Core.hs+17−23
@@ -1,6 +1,7 @@
11{-# LANGUAGE ConstraintKinds #-}
22{-# LANGUAGE FlexibleContexts #-}
33{-# LANGUAGE FlexibleInstances #-}
4+{-# LANGUAGE LambdaCase #-}
45{-# LANGUAGE MultiParamTypeClasses #-}
56{-# LANGUAGE RankNTypes #-}
67{-# LANGUAGE UndecidableInstances #-} -- XXX
@@ -253,10 +254,9 @@ runqueueLIFO sv q = run
253254 yield a Nothing = sendit a >> run
254255 yield a (Just r) = sendit a >> (runStream r) (Just sv) run yield
255256
256− dequeue = liftIO $ atomicModifyIORefCAS q $ \ ms ->
257− case ms of
258− [] -> ([], Nothing)
259− x : xs -> (xs, Just x)
257+ dequeue = liftIO $ atomicModifyIORefCAS q $ \case
258+ [] -> ([], Nothing)
259+ x : xs -> (xs, Just x)
260260
261261{-# INLINE enqueueFIFO #-}
262262enqueueFIFO :: LinkedQueue (Stream m a) -> Stream m a -> IO ()
@@ -289,7 +289,7 @@ runqueueFIFO sv q = run
289289{-# NOINLINE addThread #-}
290290addThread :: MonadIO m => SVar m a -> ThreadId -> m ()
291291addThread sv tid =
292− liftIO $ modifyIORef (runningThreads sv) $ (\s -> S.insert tid s)
292+ liftIO $ modifyIORef (runningThreads sv) (S.insert tid)
293293
294294{-# INLINE delThread #-}
295295delThread :: MonadIO m => SVar m a -> ThreadId -> m ()
@@ -298,8 +298,7 @@ delThread sv tid =
298298
299299{-# INLINE allThreadsDone #-}
300300allThreadsDone :: MonadIO m => SVar m a -> m Bool
301−allThreadsDone sv = liftIO $ do
302− readIORef (runningThreads sv) >>= return . S.null
301+allThreadsDone sv = liftIO $ S.null <$> readIORef (runningThreads sv)
303302
304303{-# NOINLINE handleChildException #-}
305304handleChildException :: MonadIO m => SVar m a -> SomeException -> m ()
@@ -326,8 +325,8 @@ sendWorkerWait sv = do
326325 output <- liftIO $ readIORef (outputQueue sv)
327326 when (null output) $ do
328327 done <- queueEmpty sv
329− if (not done)
330− then (pushWorker sv) >> sendWorkerWait sv
328+ if not done
329+ then pushWorker sv >> sendWorkerWait sv
331330 else void (liftIO $ takeMVar (doorBell sv))
332331
333332-- | Pull a stream from an SVar.
@@ -381,13 +380,13 @@ getFifoSVar ctype = do
381380 running <- newIORef S.empty
382381 q <- newQ
383382 let sv =
384− SVar { outputQueue = outQ
383+ SVar { outputQueue = outQ
385384 , doorBell = outQMv
386385 , runningThreads = running
387386 , runqueue = runqueueFIFO sv q
388387 , enqueue = pushL q
389388 , queueEmpty = liftIO $ nullQ q
390− , svarStyle = ctype
389+ , svarStyle = ctype
391390 }
392391 in return sv
393392
@@ -397,30 +396,25 @@ getLifoSVar ctype = do
397396 outQMv <- newEmptyMVar
398397 running <- newIORef S.empty
399398 q <- newIORef []
400− let checkEmpty = liftIO (readIORef q) >>= return . null
399+ let checkEmpty = null <$> liftIO (readIORef q)
401400 let sv =
402− SVar { outputQueue = outQ
401+ SVar { outputQueue = outQ
403402 , doorBell = outQMv
404403 , runningThreads = running
405404 , runqueue = runqueueLIFO sv q
406405 , enqueue = enqueueLIFO q
407406 , queueEmpty = checkEmpty
408− , svarStyle = ctype
407+ , svarStyle = ctype
409408 }
410409 in return sv
411410
412411-- | Create a new empty SVar.
413412newEmptySVar :: MonadAsync m => SVarStyle -> m (SVar m a)
414413newEmptySVar style = do
415− sv <- liftIO $
414+ liftIO $
416415 case style of
417− SVarStyle _ FIFO -> do
418− c <- getFifoSVar style
419− return c
420− SVarStyle _ LIFO -> do
421− c <- getLifoSVar style
422− return c
423− return sv
416+ SVarStyle _ FIFO -> getFifoSVar style
417+ SVarStyle _ LIFO -> getLifoSVar style
424418
425419-- | Create a new SVar and enqueue one stream computation on it.
426420newStreamVar1 :: MonadAsync m => SVarStyle -> Stream m a -> m (SVar m a)
@@ -534,7 +528,7 @@ withNewSVar2 style m1 m2 = Stream $ \_ stp yld -> do
534528{-# INLINE joinStreamVar2 #-}
535529joinStreamVar2 :: MonadAsync m
536530 => 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 ->
538532 case st of
539533 Just sv | svarStyle sv == style ->
540534 liftIO ((enqueue sv) m2) >> (runStream m1) st stp yld
src/Streamly/Prelude.hs+22−26
@@ -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
src/Streamly/Streams.hs+3−3
@@ -94,7 +94,7 @@ import Control.Monad.Reader.Class (MonadReader(..))
9494import Control.Monad.State.Class (MonadState(..))
9595import Control.Monad.Trans.Class (MonadTrans)
9696import Data.Semigroup (Semigroup(..))
97−import Prelude hiding (drop, take, zipWith)
97+import Prelude hiding (zipWith)
9898import Streamly.Core
9999
100100------------------------------------------------------------------------------
@@ -117,7 +117,7 @@ cons a r = fromStream $ scons a (Just (toStream r))
117117
118118-- | An empty stream.
119119nil :: Streaming t => t m a
120−nil = fromStream $ snil
120+nil = fromStream snil
121121
122122-- | Build a stream from its church encoding. The function passed maps
123123-- directly to the underlying representation of the stream type. The second
@@ -483,7 +483,7 @@ parbind par m f = go m
483483 Stream $ \ctx stp yld ->
484484 let run x = (runStream x) ctx stp yld
485485 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
487487 in g Nothing stp yield
488488
489489instance MonadAsync m => Monad (AsyncT m) where
src/Streamly/Time.hs+1−1
@@ -49,7 +49,7 @@ withClock clock freq action = do
4949 action localTime
5050 when (delay > 0) $ threadDelay delay
5151
52− if (n == freq)
52+ if n == freq
5353 then do
5454 (t, newTick, newDelay) <- adjustClock lastAdj localTime delay
5555 go t newDelay newTick (localTime + newTick) 0