diff options
author | Alexander Foremny <aforemny@posteo.de> | 2024-01-11 03:27:17 +0100 |
---|---|---|
committer | Alexander Foremny <aforemny@posteo.de> | 2024-01-11 08:12:02 +0100 |
commit | 75ace0b26691848b9a16a9a0ec1880110e8262b8 (patch) | |
tree | befda6536685ebc0472f5b0273ad25fdf8eb0ae4 /app/Parallel.hs | |
parent | 7bdf16be84b368655ce2ee3d9ab6bf185dfb59b5 (diff) |
feat: add `check` cmd
Diffstat (limited to 'app/Parallel.hs')
-rw-r--r-- | app/Parallel.hs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/app/Parallel.hs b/app/Parallel.hs new file mode 100644 index 0000000..02252ff --- /dev/null +++ b/app/Parallel.hs @@ -0,0 +1,22 @@ +{-# OPTIONS_GHC -fno-warn-name-shadowing #-} + +module Parallel + ( streamsOf, + parMapM, + ) +where + +import Control.Concurrent.ParallelIO.Local (parallel, withPool) +import GHC.Conc (numCapabilities) + +streamsOf :: Int -> [a] -> [[a]] +streamsOf 1 xs = [xs] +streamsOf n xs + | n > 0 = [everyN k xs | k <- [0 .. n - 1]] + | otherwise = [] + where + everyN k xs = map snd $ filter ((== k) . (`mod` n) . fst) $ zip [0 ..] xs + +parMapM :: (a -> IO b) -> [a] -> IO [b] +parMapM f xs = do + withPool numCapabilities $ \pool -> parallel pool (map f xs) |