Skip to content

Instantly share code, notes, and snippets.

@djspiewak
Created October 22, 2019 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djspiewak/d0d7a50fa29015c119e66c20b417f4e4 to your computer and use it in GitHub Desktop.
Save djspiewak/d0d7a50fa29015c119e66c20b417f4e4 to your computer and use it in GitHub Desktop.
def both[F[_]: Concurrent, A, B](s1: Stream[F, A], s2: Stream[F, B]): Stream[F, (A, B)] = {
def awaitCombine(s: Stream[F, Either[A, B]]): Pull[F, (A, B), Unit] =
s.pull.uncons1 flatMap {
case Some((Left(a), tail)) =>
tail.pull.uncons1 flatMap {
case Some((Right(b), tail)) =>
Pull.output1((a, b)) >> awaitCombine(tail)
case None =>
Pull.done
}
case Some((Right(b), tail)) =>
tail.pull.uncons1 flatMap {
case Some((Left(a), tail)) =>
Pull.output1((a, b)) >> awaitCombine(tail)
case None =>
Pull.done
}
case None =>
Pull.done
}
awaitCombine(s1.either(s2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment