Created
September 20, 2017 10:25
-
-
Save esoeylemez/ab949eda974e60779745ab7f672d073b 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.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