Skip to content

Commit

Permalink
Merge pull request #1509 from zakolenko/compile-to-map
Browse files Browse the repository at this point in the history
Added stream.compile.toMap syntax for compiling to a Map
  • Loading branch information
mpilquist committed Jun 13, 2019
2 parents 2ce2c2f + 40b9f91 commit cb349bb
Showing 1 changed file with 21 additions and 0 deletions.
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 @@ -4245,6 +4245,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

0 comments on commit cb349bb

Please sign in to comment.