Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- }
- }
Add Comment
Please, Sign In to add comment