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

Add ZSink.fromFunctionM #2112

Merged
merged 1 commit into from Nov 3, 2019
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions streams-tests/jvm/src/test/scala/zio/stream/SinkSpec.scala
Expand Up @@ -1041,6 +1041,14 @@ object SinkSpec
.runCollect,
equalTo(List("1", "2", "3", "4", "5"))
)
},
testM("fromFunctionM") {
assertM(
Stream("1", "2", "3", "4", "5")
.transduce(Sink.fromFunctionM[Throwable, String, Int](s => Task(s.toInt)))
.runCollect,
equalTo(List(1, 2, 3, 4, 5))
)
}
),
testM("fromOutputStream") {
Expand Down
6 changes: 6 additions & 0 deletions streams/shared/src/main/scala/zio/stream/Sink.scala
Expand Up @@ -191,6 +191,12 @@ object Sink extends Serializable {
final def fromFunction[A, B](f: A => B): Sink[Unit, Nothing, A, B] =
ZSink.fromFunction(f)

/**
* see [[ZSink.fromFunctionM]]
*/
final def fromFunctionM[E, A, B](f: A => ZIO[Any, E, B]): Sink[Option[E], Nothing, A, B] =
ZSink.fromFunctionM(f)

/**
* see [[ZSink.halt]]
*/
Expand Down
6 changes: 6 additions & 0 deletions streams/shared/src/main/scala/zio/stream/ZSink.scala
Expand Up @@ -1495,6 +1495,12 @@ object ZSink extends ZSinkPlatformSpecific with Serializable {
final def fromFunction[A, B](f: A => B): ZSink[Any, Unit, Nothing, A, B] =
identity.map(f)

/**
* Creates a sink that effectfully transforms incoming values.
*/
final def fromFunctionM[R, E, A, B](f: A => ZIO[R, E, B]): ZSink[R, Option[E], Nothing, A, B] =
identity.mapError(_ => None).mapM(f(_).mapError(Some(_)))

/**
* Creates a sink halting with a specified cause.
*/
Expand Down