Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def mapUnfold[B](f: A => B): Stream[B] =
- Stream.unfold(unCons) {
- case Some((h, t)) => Some(f(h), t.unCons)
- case _ => None
- }
- def takeUnfold(n: Int): Stream[A] =
- Stream.unfold((unCons, n)) {
- case (Some((h, t)), n) if n > 0 => Some(h, (t.unCons, n - 1))
- case _ => None
- }
- def takeWhileUnfold(p: A => Boolean): Stream[A] =
- Stream.unfold(unCons) {
- case Some((h, t)) if p(h) => Some(h, t.unCons)
- case _ => None
- }
- def zipUnfold[B](that: Stream[B]): Stream[(A, B)] =
- Stream.unfold((unCons, that.unCons)) {
- case (Some((x, xs)), Some((y, ys))) => Some((x, y), (xs.unCons, ys.unCons))
- case _ => None
- }
- def zipAllUnfold[AA >: A, B](that: Stream[B], thisElem: AA, thatElem: B): Stream[(AA, B)] =
- Stream.unfold(unCons, that.unCons) {
- case (Some((x, xs)), Some((y, ys))) => Some((x, y), (xs.unCons, ys.unCons))
- case (Some((x, xs)), None) => Some((x, thatElem), (xs.unCons, None))
- case (None, Some((y, ys))) => Some((thisElem, y), (None, ys.unCons))
- case _ => None
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement