Last active
August 29, 2015 13:57
-
-
Save rkrzewski/9647088 to your computer and use it in GitHub Desktop.
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 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