Created
July 18, 2012 14:50
-
-
Save bmjames/3136643 to your computer and use it in GitHub Desktop.
An enumeratee which takes every Nth element and discards the rest
This file contains hidden or 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
def takeEveryNth[E, F[_]:Monad](n: Int): EnumerateeT[E, E, F] = | |
new EnumerateeT[E, E, F] { | |
def apply[A] = { | |
def loop(m: Int) = step(m) andThen cont[E, F, StepT[E, F, A]] | |
def step(m: Int): (Input[E] => IterateeT[E, F, A]) => (Input[E] => IterateeT[E, F, StepT[E, F, A]]) = { | |
k => in => in( | |
empty = cont(step(m)(k)), | |
eof = done(scont(k), in), | |
el = e => if (m == n) k(in) >>== doneOr(loop(1)) | |
else cont(step(m+1)(k)) | |
) | |
} | |
doneOr(loop(1)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment