Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Not that I'm banging the same drum again but...</p> <p>A solution for the problem where we have a number of processes which may produce a successful output, or fail with some error message. The goal is to aggregate the successful outcomes, if all processes produce a success and if one or more fails, to aggregate all error messages.</p> <p>This can be solved by <strong>scalaz</strong> validation: <em>firstly, setup some imports</em></p> <pre><code>scala&gt; import scalaz._; import Scalaz._ import scalaz._ import Scalaz._ </code></pre> <p>Now let's define our "processes"</p> <pre><code>scala&gt; def fooI(s : String) : ValidationNEL[Exception, Int] = s.parseInt.liftFailNel fooI: (s: String)scalaz.Scalaz.ValidationNEL[Exception,Int] scala&gt; def fooF(s : String) : ValidationNEL[Exception, Float] = s.parseFloat.liftFailNel fooF: (s: String)scalaz.Scalaz.ValidationNEL[Exception,Float] scala&gt; def fooB(s : String) : ValidationNEL[Exception, Boolean] = s.parseBoolean.liftFailNel fooB: (s: String)scalaz.Scalaz.ValidationNEL[Exception,Boolean] </code></pre> <p>Now use <code>Applicative</code> to aggregate the failures/successes:</p> <pre><code>scala&gt; def attempt(ss : String*) = (fooI(ss(0)) &lt;|**|&gt; (fooF(ss(1)), fooB(ss(2)))) match { | case Success((i, f, b)) =&gt; println("Found " + i + " " + f + " " + b) | case Failure(es) =&gt; es foreach println | } attempt: (ss: String*)Unit </code></pre> <p>Now let's try for some failures:</p> <pre><code>scala&gt; attempt("a", "b", "true") java.lang.NumberFormatException: For input string: "a" java.lang.NumberFormatException: For input string: "b" </code></pre> <p>Now let's try for success:</p> <pre><code>scala&gt; attempt("1", "2.3", "false") Found 1 2.3 false </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload