Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. //Say we have a function which returns a custom error
  2. def process(line: String, max: Int): Try[Int] = {
  3. if(line.length < max) Success(line.toInt) else Failure(new IllegalArgumentException(s"$line is longer than $max"))
  4. }
  5.  
  6. //and we want to process a stream of them, but stop when there is an error
  7. def pipe[F[_]](max: Int)(implicit F: Async[F]): Pipe[F, String, Int] = {
  8. stream => stream.flatMap { line =>
  9. process(line, max) match {
  10. case Success(value) => Stream.pure(value)
  11. case Failure(e) => Stream.fail(e)
  12. }
  13. }
  14. }
  15.  
  16. val myPipe = pipe(3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement