4

I know that \/ (disjunction type) in scalaz can be used to "fail fast", i.e. return the first failure in a sequence of functions calls. What if I want to "fail slow", i.e. accumulate the failures ? I can use Validation of scalaz.

Now I wonder if \/ (disjunction type) provides any built-in support for accumulating failures.

Michael
  • 41,026
  • 70
  • 193
  • 341
  • 1
    You can always implement either behavior for either type—the issue is just which you get for free when you sequence through the type's applicative functor. – Travis Brown Mar 31 '14 at 10:09
  • @TravisBrown You are right (and I will update the question). I wonder if `\/` provides any _built-in support_ for failures accumulation. It looks like it does since it implements `ap` method ... but I still do not know exactly how to use it. – Michael Mar 31 '14 at 10:59
  • 2
    `\/`'s applicative functor is fail-fast: if both `x` and `y` fail in `(x |@| y)` you'll only see the error for `x`. – Travis Brown Mar 31 '14 at 11:38
  • 1
    You might look at `+++` and `\/`'s monoid instance, which offer a kind of failure accumulation, but it's not nearly as flexible as what you get with `Validation`. – Travis Brown Mar 31 '14 at 11:54
  • Thank you. I got it :) Now I am thinking when one will use it as a monad and when as a applicative functor. – Michael Mar 31 '14 at 13:53
  • 2
    For any well-behaved type the applicative and monadic behavior should be consistent—that's why `Validation` is no longer a monad. – Travis Brown Mar 31 '14 at 13:55
  • I see. I wonder why `\/` is both a monad and applicative. Should I use it as an applicative _instead of_ using it as a monad (as less powerful abstraction). – Michael Mar 31 '14 at 13:56
  • 2
    Sometimes you need monadic sequencing—see my question [here](http://stackoverflow.com/q/20065853/334519) for some discussion. When you don't need it, sticking with applicative syntax is generally a good policy. – Travis Brown Mar 31 '14 at 14:04
  • Thank you. It is clearer now. – Michael Mar 31 '14 at 14:19

0 Answers0