Skip to content

Instantly share code, notes, and snippets.

@huntc
Created February 9, 2018 00:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huntc/983433b14fefaa43cab08f9b966e31db to your computer and use it in GitHub Desktop.
Save huntc/983433b14fefaa43cab08f9b966e31db to your computer and use it in GitHub Desktop.
import akka.NotUsed
import akka.stream.scaladsl.Flow
case object NotZero
// Emits either an Int when zero, or a domain object indicating
// that the Int is non zero. Use this approach instead of relying
// on exceptions. Exceptions are for exceptional conditions,
// largely unanticipated, and they will terminate a stream.
val flow: Flow[Int, Either[NotZero.type, Int], NotUsed] =
Flow[Int]
.map(n => if (n == 0) Right(n) else Left(NotZero))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment