title: Validation versus disjunction
body: <p>Suppose I want to write a method with the following signature:</p>
  
  <pre><code>def parse(input: List[(String, String)]):
    ValidationNel[Throwable, List[(Int, Int)]]
  </code></pre>
  
  <p>For each pair of strings in the input, it needs to verify that both members can be parsed as integers and that the first is smaller than the second. It then needs to return the integers, accumulating any errors that turn up.</p>
  
  <p>First I'll define an error type:</p>
  
  <pre><code>import scalaz._, Scalaz._
  
  case class InvalidSizes(x: Int, y: Int) extends Exception(
    s"Error: $x is not smaller than $y!"
  )
  </code></pre>
  
  <p>Now I can implement my method as follows:</p>
  
  <pre><code>def checkParses(p: (String, String)):
    ValidationNel[NumberFormatException, (Int, Int)] =
    p.bitraverse[
      ({ type L[x] = ValidationNel[NumberFormatException, x] })#L, Int, Int
    ](
      _.parseInt.toValidationNel,
      _.parseInt.toValidationNel
    )
  
  def checkValues(p: (Int, Int)): Validation[InvalidSizes, (Int, Int)] =
    if (p._1 &gt;= p._2) InvalidSizes(p._1, p._2).failure else p.success
  
  def parse(input: List[(String, String)]):
    ValidationNel[Throwable, List[(Int, Int)]] = input.traverseU(p =&gt;
      checkParses(p).fold(_.failure, checkValues _ andThen (_.toValidationNel))
    )
  </code></pre>
  
  <p>Or, alternatively:</p>
  
  <pre><code>def checkParses(p: (String, String)):
    NonEmptyList[NumberFormatException] \/ (Int, Int) =
    p.bitraverse[
      ({ type L[x] = ValidationNel[NumberFormatException, x] })#L, Int, Int
    ](
      _.parseInt.toValidationNel,
      _.parseInt.toValidationNel
    ).disjunction
  
  def checkValues(p: (Int, Int)): InvalidSizes \/ (Int, Int) =
    (p._1 &gt;= p._2) either InvalidSizes(p._1, p._2) or p
  
  def parse(input: List[(String, String)]):
    ValidationNel[Throwable, List[(Int, Int)]] = input.traverseU(p =&gt;
      checkParses(p).flatMap(s =&gt; checkValues(s).leftMap(_.wrapNel)).validation
    )
  </code></pre>
  
  <p>Now for whatever reason the first operation (validating that the pairs parse as strings) <em>feels</em> to me like a validation problem, while the second (checking the values) <em>feels</em> like a disjunction problem, and it <em>feels</em> like I need to compose the two monadically (which suggests that I should be using <code>\/</code>, since <code>ValidationNel[Throwable, _]</code> doesn't have a monad instance).</p>
  
  <p>In my first implementation, I use <code>ValidationNel</code> throughout and then <code>fold</code> at the end as a kind of fake <code>flatMap</code>. In the second, I bounce back and forth between <code>ValidationNel</code> and <code>\/</code> as appropriate depending on whether I need error accumulation or monadic binding. They produce the same results.</p>
  
  <p>I've used both approaches in real code, and haven't yet developed a preference for one over the other. Am I missing something? <em>Should</em> I prefer one over the other?</p>