Created
September 20, 2017 10:13
-
-
Save esoeylemez/6c0d346d1a026fd3d8261a4bc16847cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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