Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Say we have a function which returns a custom error
- def process(line: String, max: Int): Try[Int] = {
- if(line.length < max) Success(line.toInt) else Failure(new IllegalArgumentException(s"$line is longer than $max"))
- }
- //and we want to process a stream of them, but stop when there is an error
- def pipe[F[_]](max: Int)(implicit F: Async[F]): Pipe[F, String, Int] = {
- stream => stream.flatMap { line =>
- process(line, max) match {
- case Success(value) => Stream.pure(value)
- case Failure(e) => Stream.fail(e)
- }
- }
- }
- val myPipe = pipe(3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement