More idiomatic (monadic?) way to express this Scala

I'm not sure you can use monads as at each step you have two alternatives (exception or result) and to be faithful to your original code, on exception you don't want to be calling the fB or fC functions.

I was not able to elegantly remove the duplication of default values so I left it as I think it's clearer. Here is my non-monadic version based on either.fold and control.Exception:

def test(s : String) = {
  import util.control.Exception._
  val args = 
    allCatch.either(fA(s)).fold(err => (0, 0.0, false), a => 
      allCatch.either(fB(s, a)).fold(err => (a, 0.0, false), b =>
        allCatch.either(fC(s, a, b)).fold(err => (a, b, false), c =>
          (a, b, c))))
  (result _).tupled(args)
}

These types of problems are just what Try aims to solve a bit more monadically (than nested try/catch blocks).

Try represents a computation that may either result in an exception, or return a successfully computed value. It has two subclasses for these-- Success and Failure.

Very funny that this question popped up when it did-- a few days ago, I finished up some additions and refactoring to scala.util.Try, for the 2.10 release and this SO question helps to illustrate an important use-case for a combinator that we eventually decided to include; transform.

(As of writing this, transform is currently in the nightly and will be in Scala from 2.10-M5 onward, due out today or tomorrow. More info about Try and usage examples can be found in the nightly docs)

With transform (by nesting them), this can be implemented using Trys as follows:

def test(s: String): Double = {
  Try(fA(s)).transform(
    ea => Success(result(0, 0.0, false)), a => Try(fB(s, a)).transform(
      eb => Success(result(a, 0.0, false)), b => Try(fC(s, a, b)).transform(
        ec => Success(result(a, b, false)), c => Try(result(a, b, c))
      )
    )
  ).get
}

I changed the example to use monads:

def fA(s: String) = Some(7)
def fB(i: Option[Int]) = Some(1.0)
def fC(d: Option[Double]) = true // might be false as well

def result(i: Int, d: Double, b: Boolean) = {
  if (b) d else i
}

def test(s: String) = result(fA(s).getOrElse(0), fB(fA(s)).getOrElse(0.0), fC(fB(fA(s))))

Note: The for-comprehension is interpreted as chained flatMap. So the type of res is Option[(Int, Double, Boolean)]. Therefore there is no need to write map or flatMap by yourself. The compiler does the work for you. :)

Edit

I edited my code to make it fit to all possibilitys. I will improve it, if I find a better way. Thank you for all your comments.

Tags:

Scala

Monads