3

I'd like to implement validation for a sequence of operations that all return Either[Error,Item] It should be fail-fast (in my initial need), I mean, returning Either[Error,Seq[Item]]. If there is an error, it's obvious i do not want the following operations to be performed. But in the future i may want to collect all the errors instead of returning only the first one.

I know Scalaz can do the job but for now I quite don't understand all parts of Scalaz and I'm pretty sure there's a simpler way to do it without using Scalaz, but using by-name parameters for exemple.

Is there a way to store by-name parameters in a sequence? So that i can create a sequence of by-name values that represent my operations?

I mean, some kind of type Seq[=> Either[Error,Item]] Then I could do something like calling takeWhile or collectFirst or something somilar, without all the operations being performed before the creation of the sequence? I would expect the operations to be performed only when iterating on the sequence.

Thanks

Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419

2 Answers2

2

You can indeed use a Seq[() => Either[Error, Item]] to defer the computation at collection creation time. So for example

val doSomething1: () => Either[Error, Item] = () => { println(1); Right(1) }
val doSomething2: () => Either[Error, Item] = () => { println(2); Right(2) }
val doSomething3: () => Either[Error, Item] = () => { println(3); Left("error") }
val doSomething4: () => Either[Error, Item] = () => { println(4); Right(3) }
val doSomething5: () => Either[Error, Item] = () => { println(5); Left("second error") }
val l = Seq(doSomething1, doSomething2, doSomething3, doSomething4, doSomething5)

(Items are Ints in the example and Errors are Strings)

Then you can process them lazily stopping at first failure using the following recursive function:

def processUntilFailure(l: Seq[() => Either[Error, Item]]): Either[Error, Seq[Item]] = {
  l.headOption.map(_.apply() match {
    case Left(error) => Left(error)
    case Right(item)  => processUntilFailure(l.tail).right.map(_ :+ item)
  }).getOrElse(Right(Nil))
}

So now when I run processUntilFailure(l)

scala> processUntilFailure(l)
1
2
3
res1: Either[Error,Seq[Item]] = Left(error)

If you wanted to generate a Either[Seq[String], Seq[Int]] (processing all the operations). You could do it with a little change:

def processAll(l: Seq[() => Either[Error, Item]]): Either[Seq[Error], Seq[Item]] = {
  l.headOption.map(_.apply() match {
    case Left(error) => processAll(l.tail) match {
      case Right(_) => Left(Seq(error))
      case Left(previousErrors) => Left(previousErrors :+ error)
    }
    case Right(item)  => processAll(l.tail).right.map(_ :+ item)
  }).getOrElse(Right(Nil))
}

The only change as you can see is the Left case in the pattern match. Running this one:

scala> processAll(l)
1
2
3
4
5
res0: Either[Seq[Error],Seq[Item]] = Left(List(second error, error))

processAll can be replaced with a generic foldLeft on l

val zero: Either[Seq[Error], Seq[Item]] = Right(Seq[Item]())
l.foldLeft(zero) { (errorsOrItems: Either[Seq[Error], Seq[Item]], computation: () => Either[String, Int]) =>
  computation.apply().fold(
    { (error: String) => Left(errorsOrItems.left.toOption.map(_ :+ error).getOrElse(Seq(error))) },
    { (int: Int) => errorsOrItems.right.map(_ :+ int) })
}

processUntilFailure can as well but not easily. Since aborting early from a fold is tricky. Here's a good answer about other possible approaches when you find yourself needing to do that.

Community
  • 1
  • 1
Ratan Sebastian
  • 1,892
  • 14
  • 23
  • I think the problem is that the Left("second error") is computed, while i do not want it to be computed, and only return the 1st error found :) – Sebastien Lorber Oct 21 '12 at 12:59
  • It isn't in the `processUntilFailure` function. When you run it with the list of computations only 1,2 and 3 are printed which means that `doSomething4` and `doSomething5` are never evaluated. What am I missing here? – Ratan Sebastian Oct 21 '12 at 13:00
  • Yes that's right i misread! Finally it's kind of the same solution that has been provided by Ptharien's Flame – Sebastien Lorber Oct 21 '12 at 13:31
1

You should be able to pull this off with the type Seq[Function0[Either[Error, Item]]]. Function0 is, obviously, a zero-argument function. The rest should be self-explanatory.

Scalaz provides the type IO for exactly this purpose, so you could actually use that as well. You may not want to yet, however, if you're just beginning to work with Scalaz.

Ptharien's Flame
  • 3,246
  • 20
  • 24