Created
September 1, 2011 03:29
-
-
Save sidharthkuruvila/1185367 to your computer and use it in GitHub Desktop.
unfoldLeft
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
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