Advertisement
Guest User

Untitled

a guest
Jun 24th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.08 KB | None | 0 0
  1.   def mapUnfold[B](f: A => B): Stream[B] =
  2.     Stream.unfold(unCons) {
  3.       case Some((h, t)) => Some(f(h), t.unCons)
  4.       case _ => None
  5.     }
  6.  
  7.   def takeUnfold(n: Int): Stream[A] =
  8.     Stream.unfold((unCons, n)) {
  9.       case (Some((h, t)), n) if n > 0 => Some(h, (t.unCons, n - 1))
  10.       case _ => None
  11.     }
  12.  
  13.   def takeWhileUnfold(p: A => Boolean): Stream[A] =
  14.     Stream.unfold(unCons) {
  15.       case Some((h, t)) if p(h) => Some(h, t.unCons)
  16.       case _ => None
  17.     }
  18.   def zipUnfold[B](that: Stream[B]): Stream[(A, B)] =
  19.     Stream.unfold((unCons, that.unCons)) {
  20.       case (Some((x, xs)), Some((y, ys))) => Some((x, y), (xs.unCons, ys.unCons))
  21.       case _ => None
  22.     }
  23.  
  24.   def zipAllUnfold[AA >: A, B](that: Stream[B], thisElem: AA, thatElem: B): Stream[(AA, B)] =
  25.     Stream.unfold(unCons, that.unCons) {
  26.       case (Some((x, xs)), Some((y, ys))) => Some((x, y), (xs.unCons, ys.unCons))
  27.       case (Some((x, xs)), None) => Some((x, thatElem), (xs.unCons, None))
  28.       case (None, Some((y, ys))) => Some((thisElem, y), (None, ys.unCons))
  29.       case _ => None
  30.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement