Skip to content

Instantly share code, notes, and snippets.

@sidharthkuruvila
Created September 1, 2011 03:29
Show Gist options
  • Save sidharthkuruvila/1185367 to your computer and use it in GitHub Desktop.
Save sidharthkuruvila/1185367 to your computer and use it in GitHub Desktop.
unfoldLeft
import collection.generic.CanBuildFrom
object Unfoldable{
implicit def anyToUnfoldable[T](t:T) = new Unfoldable(t)
}
class Unfoldable[T](x: T){
def unfoldLeft[B, That](stop:T)(f:T=>(B, T))
(implicit bf: CanBuildFrom[Nothing, B, That]): That = {
val b = bf()
def unfold(v:T){
if(v != stop) {
val (nx, r) = f(v)
unfold(r)
b+=nx
}
}
unfold(x)
b.result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment