[Haskell-cafe] a sort of chunk
PICCA Frederic-Emmanuel
frederic-emmanuel.picca at synchrotron-soleil.fr
Fri Jan 17 15:57:18 UTC 2020
Hello
i end up with this
data Chunk = Chunk FilePath Int Int
deriving Show
chunkLen :: Chunk -> Int
chunkLen (Chunk _ f t) = t - f
chunks :: Int -> [Chunk] -> [[Chunk]]
chunks n cs = reverse $ map reverse $ go cs [[]] 0
where
go :: [Chunk] -> [[Chunk]] -> Int -> [[Chunk]]
go [] _ _ = []
go [x@(Chunk fn f t)] (c:cs') acc =
if acc + chunkLen x < n
then (x : c) : cs'
else go [(Chunk fn (f + n - acc) t)] ([] : ((Chunk fn f (f + n - acc)) : c) : cs') 0
go (x@(Chunk fn f t):xs) (c:cs') acc =
if acc + chunkLen x < n
then go xs ((x : c) : cs') (acc + chunkLen x)
else go ((Chunk fn (f + n - acc) t) : xs) ([] : ((Chunk fn f (f + n - acc)) : c) : cs') 0
But i do not find this that elegant...
More information about the Haskell-Cafe
mailing list