Skip to content

Instantly share code, notes, and snippets.

@esoeylemez
Created September 20, 2017 10:13
Show Gist options
  • Save esoeylemez/6c0d346d1a026fd3d8261a4bc16847cc to your computer and use it in GitHub Desktop.
Save esoeylemez/6c0d346d1a026fd3d8261a4bc16847cc to your computer and use it in GitHub Desktop.
import Control.Monad
import Data.Foldable
import Data.Sequence ((|>))
data Trav a m r = Trav (a -> m (Trav a m r)) (m r)
chunksOf :: (Applicative m) => ([a] -> m ()) -> Int -> Trav a m ()
chunksOf yield n = go mempty
where
go xs' =
Trav (\x -> do
let xs = xs' |> x
if length xs >= n
then go mempty <$ yield (toList xs)
else pure (go xs))
(when (not (null xs')) (yield (toList xs')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment