Skip to content
This repository was archived by the owner on Jun 10, 2021. It is now read-only.

Commit 1ccbadb

Browse files
committed
Implement zip
1 parent ca4870f commit 1ccbadb

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/main/scala/asyncstreams/AsyncStream.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ class AsyncStream[F[+_]: Monad, +A](private[asyncstreams] val data: F[Step[A, As
7575
def foldMap[B](f: A => B)(implicit impl: ASImpl[F], mb: Monoid[B]): F[B] = {
7676
impl.collectLeft(this)(mb.empty)((b, a) => mb.combine(b, f(a)))
7777
}
78+
79+
def zip[B](sb: AsyncStream[F, B]): AsyncStream[F, (A, B)] = AsyncStream {
80+
for {
81+
stepA <- data
82+
stepB <- sb.data
83+
} yield Step((stepA.value, stepB.value), stepA.rest zip stepB.rest)
84+
}
7885
}
7986

8087
object AsyncStream {

src/test/scala/asyncstreams/stdFuture/AsyncStreamOperations.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,13 @@ class AsyncStreamOperations extends FunSuite with Matchers {
122122

123123
await(res) shouldBe 465
124124
}
125+
126+
test("zip") {
127+
val s1 = 1 ~:: 2 ~:: 3 ~:: AsyncStream.asyncNil[Future, Int]
128+
val s2 = 4 ~:: 5 ~:: AsyncStream.asyncNil[Future, Int]
129+
130+
val res = s1 zip s2
131+
132+
await(res.to[List]) shouldBe (1, 4) :: (2, 5) :: Nil
133+
}
125134
}

0 commit comments

Comments
 (0)