score:4
Accepted answer
If you are into using Either
you could do the following. It looks nicer, if it is understandable depends on how at ease you are with either.
val either = for {
y <- f1(x).toRight(error1).right
z <- f2(y).toRight(error2).right
} yield ok(z)
either.merge
Some background:
Option.toRight
converts to a Right
(success case) if the option is defined, otherwise returns a Left
of the argument (error case).
Either.right
projects the Either
to the right, i.e. monadic operations are such that Right
is Some
, Left
is None
, but it preserves the Left
value if we are in the left case.
Either.merge
works only on Either[A, A]
and returns whatever value is in the Left
or the Right
.
Source: stackoverflow.com
Related Query
- How to match nested option values in scala
- How to get the option to create a new Scala Worksheet in IntelliJ?
- How to convert a nested scala collection to a nested Java collection
- scala style - how to avoid having lots of nested map
- How can a Scala compiler option be appended to scalacOptions in sbt?
- how to use new scala 2.8.0 nested annotations
- Avoiding deeply nested Option cascades in Scala
- How to make a nested toSet in scala in an idiomatic way?
- How to use Scala DataFrameReader option method
- How to initialize an Option array to None in Scala
- how to concatenate option in scala
- How to convert/wrap a sequence in scala to an Option[Seq] so that if the list is empty, the Option is None
- how to parse generic case class fields using scala option parser?
- How to add the "--deploy-mode cluster" option to my scala code
- How to implicitly wrap a value that can be null or an array into an Scala Option
- How to return different results from Futures in Scala without ending up in nested if statements
- How to do a nested groupBy in Scala
- How to getOrElse with another Option in Scala
- Scala syntax how to create an instance of a nested case class
- Best Practices for nested Scala Option fields?
- How to convert nested JSON to map object in scala
- Scala nested maps - how to process?
- How to track nested functions in Scala
- How to instantiate a nested generic class defined in scala from java?
- How to use monocle to modify a nested map and another field in scala
- How to pass nested scala object reference in Java?
- How do I update a nested key of a JsObject in scala
- In Scala, how to refactor codes with Option class like this?
- How could I simplify this Scala Option usage
- How to make sure an Option field in Scala is not None
More Query from same tag
- why doesn't scala recognize com.vaadin.ui.MenuBar.MenuItem
- extending scala base Vector constructor
- flatMap ignoring the result
- Scala object MODULE$
- Spark: How to merge json objects into array
- Akka Cluster Connection Refused Between Machines
- How to "force" CRC files to appear when writing csv/parquet on HDFS in Spark
- Cannot add ReactiveMongo to Play-Framework
- Why are Futures in scala realised using a collection in the companion object?
- Scala: using value field to implement a trait method
- Scala main class error
- Scala pattern matching for type
- SBT console doesn't want to launch in intelliJ
- Replacing options in Scala with default values if there is nothing there
- Akka actor forward message with continuation
- How to read HDFS file from Scala code
- Why I need a new primitive?
- toList on Range with suffix notation causes type mismatch
- Amazon AWS get object URI
- Scala flatMap, what are ms and e?
- How to generate the materialized value from the elements in Source or Flow?
- Scalaz - runnable monad
- Apache Spark: multiple outputs in one map task
- Scala using shapeless to combine higher kinded coproducts over a natural transformation
- How to read from standard input line by line?
- how to return either value or throw error in Scala
- Is it possible to call a python function from Scala(spark)
- Slick plain SQL escape PostgreSQL json function
- Avoid namespace conflicts in Java MPI-Bindings
- Understanding F-bound polymorphism in Scala