[commit: ghc] wip/nfs-locking: Add chunksOfSize helper function. (797df55)

git at git.haskell.org git at git.haskell.org
Thu Oct 26 23:06:37 UTC 2017


Repository : ssh://git@git.haskell.org/ghc

On branch  : wip/nfs-locking
Link       : http://ghc.haskell.org/trac/ghc/changeset/797df55a99ffbe2fe94bae5dc202444b294ae2d0/ghc

>---------------------------------------------------------------

commit 797df55a99ffbe2fe94bae5dc202444b294ae2d0
Author: Andrey Mokhov <andrey.mokhov at gmail.com>
Date:   Thu Jan 15 02:02:28 2015 +0000

    Add chunksOfSize helper function.


>---------------------------------------------------------------

797df55a99ffbe2fe94bae5dc202444b294ae2d0
 src/Util.hs | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/src/Util.hs b/src/Util.hs
index f91ff79..b1ff9e5 100644
--- a/src/Util.hs
+++ b/src/Util.hs
@@ -1,6 +1,7 @@
 module Util (
     module Data.Char,
-    replaceIf, replaceEq, replaceSeparators
+    replaceIf, replaceEq, replaceSeparators,
+    chunksOfSize
     ) where
 
 import Base
@@ -15,3 +16,17 @@ replaceEq from = replaceIf (== from)
 replaceSeparators :: Char -> String -> String
 replaceSeparators = replaceIf isPathSeparator
 
+-- (chunksOfSize size ss) splits a list of strings 'ss' into chunks not
+-- exceeding the given 'size'.
+chunksOfSize :: Int -> [String] -> [[String]]
+chunksOfSize _    [] = []
+chunksOfSize size ss = reverse chunk : chunksOfSize size rest
+  where
+    (chunk, rest) = go [] 0 ss
+    go chunk _         []     = (chunk, [])
+    go chunk chunkSize (s:ss) = let newSize = chunkSize + length s
+                                    (newChunk, rest) = go (s:chunk) newSize ss
+                                in
+                                if newSize > size
+                                then (chunk   , s:ss)
+                                else (newChunk, rest)



More information about the ghc-commits mailing list