Guest User

Untitled

a guest
Jan 23rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.42 KB | None | 0 0
  1. import collection.generic.CanBuildFrom
  2.  
  3. object Unfoldable{
  4. implicit def anyToUnfoldable[T](t:T) = new Unfoldable(t)
  5. }
  6. class Unfoldable[T](x: T){
  7. def unfoldLeft[B, That](stop:T)(f:T=>(B, T))
  8. (implicit bf: CanBuildFrom[Nothing, B, That]): That = {
  9. val b = bf()
  10. def unfold(v:T){
  11. if(v != stop) {
  12. val (nx, r) = f(v)
  13. unfold(r)
  14. b+=nx
  15. }
  16. }
  17. unfold(x)
  18. b.result
  19. }
  20. }
Add Comment
Please, Sign In to add comment