monad-parallel-progressbar (empty) → 0.1.0.0
raw patch · 5 files changed
+121/−0 lines, 5 filesdep +basedep +monad-paralleldep +monadIOsetup-changed
Dependencies added: base, monad-parallel, monadIO, terminal-progress-bar
Files
- CHANGES.md +5/−0
- LICENSE +22/−0
- Setup.hs +2/−0
- monad-parallel-progressbar.cabal +35/−0
- src/Control/Monad/Parallel/ProgressBar.hs +57/−0
+ CHANGES.md view
@@ -0,0 +1,5 @@+Revision history for Haskell package monad-parallel-progressbar+===++## Version 0.1.0.0+- Original version
+ LICENSE view
@@ -0,0 +1,22 @@+The MIT License (MIT)++Copyright (c) 2015 Mitsuhiro Nakamura++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.+
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ monad-parallel-progressbar.cabal view
@@ -0,0 +1,35 @@+name: monad-parallel-progressbar+version: 0.1.0.0+stability: experimental++synopsis: Parallel execution of monadic computations with a progress bar+description: This library attaches a progress bar to a subset of+ functions in "Control.Monad.Parallel".+category: Control, Monads, System, User Interfaces++author: Mitsuhiro Nakamura+maintainer: Mitsuhiro Nakamura <m.nacamura@gmail.com>+copyright: Copyright (c) 2015 Mitsuhiro Nakamura+license: MIT+license-file: LICENSE+homepage: https://github.com/mnacamura/monad-parallel-progressbar++cabal-version: >=1.10+build-type: Simple+extra-source-files: CHANGES.md++source-repository head+ type: git+ location: https://github.com/mnacamura/monad-parallel-progressbar.git++library+ exposed-modules: Control.Monad.Parallel.ProgressBar+ -- other-modules: + -- other-extensions: + build-depends: base >=4.7 && <4.8+ , monad-parallel+ , monadIO+ , terminal-progress-bar+ hs-source-dirs: src+ default-language: Haskell2010+ ghc-options: -Wall
+ src/Control/Monad/Parallel/ProgressBar.hs view
@@ -0,0 +1,57 @@+module Control.Monad.Parallel.ProgressBar+ ( sequence+ , sequence_+ , mapM+ , mapM_+ , forM+ , forM_+ ) where++import Control.Concurrent.MonadIO ( MonadIO, liftIO )+import Control.Concurrent.STM.MonadIO ( newTMVar, putTMVar, takeTMVar )+import Control.Monad ( liftM )+import Control.Monad.Parallel ( MonadParallel )+import qualified Control.Monad.Parallel as P ( sequence, sequence_ )+import Data.List ( genericLength )+import Prelude hiding ( mapM, mapM_, sequence, sequence_ )+import System.IO ( hFlush, stdout )+import System.ProgressBar ( Label, progressBar )++withProgressBar :: MonadIO m => Label -> Label -> Integer -> [m a] -> m [m a]+withProgressBar prefix postfix width ms = do+ let n = genericLength ms++ prog <- newTMVar 0+ liftIO $ do progressBar prefix postfix width 0 n+ hFlush stdout+ return $ flip map ms $ \m -> do+ a <- m+ i <- liftM (+1) $ takeTMVar prog+ liftIO $ do progressBar prefix postfix width i n+ hFlush stdout+ putTMVar prog i+ return a++-- | Execute the actions in parallel with a progress bar. For detail of the+-- progress bar, see "System.ProgressBar".+sequence :: (MonadIO m, MonadParallel m)+ => Label -- ^ Prefixed label+ -> Label -- ^ Postfixed label+ -> Integer -- ^ Total progress bar width in characters+ -> [m a] -> m [a]+sequence prefix postfix width ms = P.sequence =<< withProgressBar prefix postfix width ms++sequence_ :: (MonadIO m, MonadParallel m) => Label -> Label -> Integer -> [m a] -> m ()+sequence_ prefix postfix width ms = P.sequence_ =<< withProgressBar prefix postfix width ms++mapM :: (MonadIO m, MonadParallel m) => Label -> Label -> Integer -> (a -> m b) -> [a] -> m [b]+mapM prefix postfix width f = sequence prefix postfix width . map f++mapM_ :: (MonadIO m, MonadParallel m) => Label -> Label -> Integer -> (a -> m ()) -> [a] -> m ()+mapM_ prefix postfix width f = sequence_ prefix postfix width . map f++forM :: (MonadIO m, MonadParallel m) => Label -> Label -> Integer -> [a] -> (a -> m b) -> m [b]+forM prefix postfix width = flip (mapM prefix postfix width)++forM_ :: (MonadIO m, MonadParallel m) => Label -> Label -> Integer -> [a] -> (a -> m ()) -> m ()+forM_ prefix postfix width = flip (mapM_ prefix postfix width)