Skip to content

Instantly share code, notes, and snippets.

@dchrusciak
Created March 19, 2014 17:20
Show Gist options
  • Save dchrusciak/9646696 to your computer and use it in GitHub Desktop.
Save dchrusciak/9646696 to your computer and use it in GitHub Desktop.
def encodeDirect[T](ts: Seq[T]): Seq[(Int, T)] = {
def encode(xs: Seq[T], acc: Seq[(Int, T)]): Seq[(Int, T)] = {
if (ts.isEmpty) acc
else {
val (packed, next) = ts.span(_ == ts.head)
encode(next, (packed.length, packed.head) +: acc)
}
}
encode(ts, Nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment