Skip to content

Instantly share code, notes, and snippets.

@bmjames
Created July 18, 2012 14:50
Show Gist options
  • Save bmjames/3136643 to your computer and use it in GitHub Desktop.
Save bmjames/3136643 to your computer and use it in GitHub Desktop.
An enumeratee which takes every Nth element and discards the rest
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