Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added stream.compile.toMap syntax for compiling to a Map #1509

Merged
merged 2 commits into from
Jun 13, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions core/shared/src/main/scala/fs2/Stream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4246,6 +4246,27 @@ object Stream extends StreamLowPriority {
*/
def toVector: G[Vector[O]] =
to[Vector]

/**
* Compiles this stream in to a value of the target effect type `F` by logging
* the output values to a `Map`.
*
* When this method has returned, the stream has not begun execution -- this method simply
* compiles the stream down to the target effect type.
*
* @example {{{
* scala> import cats.effect.IO
* scala> Stream.range(0,100).map(i => i -> i).take(5).covary[IO].compile.toMap.unsafeRunSync.mkString(", ")
* res0: String = 0 -> 0, 1 -> 1, 2 -> 2, 3 -> 3, 4 -> 4
* }}}
*/
def toMap[K, V](implicit ev: O <:< (K, V)): G[Map[K, V]] = {
val _ = ev
compiler(self.asInstanceOf[Stream[F, (K, V)]], () => Map.newBuilder[K, V])(
_ ++= _.iterator,
_.result
)
}
}

/**
Expand Down