Skip to content

Instantly share code, notes, and snippets.

@rkrzewski
Last active August 29, 2015 13:57
Show Gist options
  • Save rkrzewski/9647088 to your computer and use it in GitHub Desktop.
Save rkrzewski/9647088 to your computer and use it in GitHub Desktop.
def grouped[T](ts : Seq[T], n: Int) = new Iterator[Seq[T]] {
var cur : Seq[T] = ts
def hasNext = !cur.isEmpty
def next(): Seq[T] = {
val (a, b) = cur.splitAt(n)
cur = b
a
}
}
def takeWhile[T](ts: Seq[T])(p: T => Boolean): Seq[T] = {
@tailrec
def go(ts: Seq[T], acc: Seq[T]): Seq[T] = ts match {
case e :: es if p(e) => go(es, e +: acc)
case _ => acc.reverse
}
go(ts, Seq())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment