From 7ddecedd11459300a371b0479158473a4b4bbd6b Mon Sep 17 00:00:00 2001 From: Gurkenglas Date: Wed, 3 Aug 2016 13:50:06 +0200 Subject: [PATCH 1/2] Refactor exhaustTChan Exterminate recursion! --- src/PreludeExtra.hs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/PreludeExtra.hs b/src/PreludeExtra.hs index 6a07f0e..6facc0a 100644 --- a/src/PreludeExtra.hs +++ b/src/PreludeExtra.hs @@ -14,6 +14,8 @@ import Data.Foldable as Exports import Control.Arrow as Exports import Control.Concurrent as Exports import Control.Concurrent.STM as Exports hiding (atomically) +import Safe +import Control.Monad.Trans.Maybe import Data.Char as Exports import Text.Read as Exports (readMaybe) @@ -68,9 +70,7 @@ useTraverseM_ aLens f = traverseM_ (use aLens) f exhaustTChan :: TChan a -> STM [a] -exhaustTChan chan = tryReadTChan chan >>= \case - Just a -> (a:) <$> exhaustTChan chan - Nothing -> return [] +exhaustTChan = fmap (fromJustNote "many") . runMaybeT . many . MaybeT . tryReadTChan atomically :: MonadIO m => STM a -> m a atomically = liftIO . STM.atomically From 2c8d92c93c42af744b838059cda00d9f3010c878 Mon Sep 17 00:00:00 2001 From: Gurkenglas Date: Wed, 3 Aug 2016 14:16:09 +0200 Subject: [PATCH 2/2] Reexport imports That's the style in this file and I just noticed Safe is useful elsewhere. --- src/PreludeExtra.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PreludeExtra.hs b/src/PreludeExtra.hs index 6facc0a..d076917 100644 --- a/src/PreludeExtra.hs +++ b/src/PreludeExtra.hs @@ -14,8 +14,8 @@ import Data.Foldable as Exports import Control.Arrow as Exports import Control.Concurrent as Exports import Control.Concurrent.STM as Exports hiding (atomically) -import Safe -import Control.Monad.Trans.Maybe +import Safe as Exports +import Control.Monad.Trans.Maybe as Exports import Data.Char as Exports import Text.Read as Exports (readMaybe)