Haskell split a list into fixed-size batches
Haskell
import Data.List (unfoldr)
chunksOf :: Int -> [a] -> [[a]]
chunksOf n
| n <= 0 = []
| otherwise = unfoldr step
where
step [] = Nothing
step xs = Just (splitAt n xs)
batchIndex :: Int -> [a] -> [(Int, [a])]
batchIndex n = zip [0 ..] . chunksOf n