Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is relatively concise with little "added code". It is still sort of wonky though because it ignores the successful result of applying <code>allDigits</code>.</p> <pre><code>scala&gt; val validated = for { | x &lt;- allDigits | y &lt;- maxSizeOfTen | } yield x *&gt; y validated: String =&gt; scalaz.Validation[scalaz.NonEmptyList[String],String] = &lt;function1&gt; scala&gt; val validatedToInt = (str: String) =&gt; validated(str) flatMap(toInt) validatedToInt: String =&gt; scalaz.Validation[scalaz.NonEmptyList[String],Int] = &lt;function1&gt; scala&gt; validatedToInt("10") res25: scalaz.Validation[scalaz.NonEmptyList[String],Int] = Success(10) </code></pre> <p>Alternatively you could keep both of the outputs of <code>allDigits</code> and <code>maxSizeOfTen</code>. </p> <pre><code>val validated2 = for { x &lt;- allDigits y &lt;- maxSizeOfTen } yield x &lt;|*|&gt; y </code></pre> <p>I'm curious if someone else could come up with a better way to combine these. It's not really composition...</p> <pre><code>val validatedToInt = (str: String) =&gt; validated2(str) flatMap(_ =&gt; toInt(str)) </code></pre> <p>Both <code>validated</code> and <code>validated2</code> accumulate failures as shown below:</p> <pre><code>scala&gt; def allDigits: (String) =&gt; ValidationNEL[String, String] = _ =&gt; failure(NonEmptyList("All Digits Fail")) allDigits: String =&gt; scalaz.Scalaz.ValidationNEL[String,String] scala&gt; def maxSizeOfTen: (String) =&gt; ValidationNEL[String, String] = _ =&gt; failure(NonEmptyList("max &gt; 10")) maxSizeOfTen: String =&gt; scalaz.Scalaz.ValidationNEL[String,String] scala&gt; val validated = for { | x &lt;- allDigits | y &lt;- maxSizeOfTen | } yield x *&gt; y validated: String =&gt; scalaz.Validation[scalaz.NonEmptyList[String],String] = &lt;function1&gt; scala&gt; val validated2 = for { | x &lt;- allDigits | y &lt;- maxSizeOfTen | } yield x &lt;|*|&gt; y validated2: String =&gt; scalaz.Validation[scalaz.NonEmptyList[String],(String, String)] = &lt;function1&gt; scala&gt; validated("ten") res1: scalaz.Validation[scalaz.NonEmptyList[String],String] = Failure(NonEmptyList(All Digits Fail, max &gt; 10)) scala&gt; validated2("ten") res3: scalaz.Validation[scalaz.NonEmptyList[String],(String, String)] = Failure(NonEmptyList(All Digits Fail, max &gt; 10)) </code></pre>
    singulars
    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.
 

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