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

Commit 053883a

Browse files
committed
Implement foldMap
1 parent a0178b5 commit 053883a

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/main/scala/asyncstreams/AsyncStream.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package asyncstreams
22

3+
import cats.kernel.Monoid
34
import cats.{Alternative, Monad}
45
import cats.syntax.applicative._
56
import cats.syntax.flatMap._
67
import cats.syntax.functor._
8+
import cats.syntax.semigroup._
9+
import cats.syntax.monoid._
710
import cats.syntax.semigroupk._
811

912
import scala.annotation.tailrec
@@ -71,6 +74,10 @@ class AsyncStream[F[+_]: Monad, +A](private[asyncstreams] val data: F[Step[A, As
7174
def findF(p: A => F[Boolean])(implicit impl: ASImpl[F]): F[Option[A]] = impl.findF(this, p)
7275

7376
def partition(p: A => Boolean): (AsyncStream[F, A], AsyncStream[F, A]) = (filter(p), filter(p.andThen(!_)))
77+
78+
def foldMap[B](f: A => B)(implicit impl: ASImpl[F], mb: Monoid[B]): F[B] = {
79+
impl.collectLeft(this)(mb.empty)((b, a) => mb.combine(b, f(a)))
80+
}
7481
}
7582

7683
object AsyncStream {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package asyncstreams.stdFuture
22

33
import asyncstreams._
44
import asyncstreams.impl._
5+
import cats.instances.int._
56
import cats.instances.future._
67
import cats.syntax.applicative._
78
import org.scalatest.{FunSuite, Matchers}
@@ -115,4 +116,10 @@ class AsyncStreamOperations extends FunSuite with Matchers {
115116

116117
await(res2) should be (None)
117118
}
119+
120+
test("foldMap") {
121+
val res = stream.foldMap(identity)
122+
123+
await(res) shouldBe 465
124+
}
118125
}

0 commit comments

Comments
 (0)