Skip to content

Instantly share code, notes, and snippets.

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