Note that there are some explanatory texts on larger screens.

plurals
  1. POFunction syntax puzzler in scalaz
    text
    copied!<p>Following watching <a href="http://vimeo.com/10482466" rel="noreferrer">Nick Partidge's presentation</a> on deriving <a href="http://github.com/scalaz/scalaz" rel="noreferrer">scalaz</a>, I got to looking at this example, which is just awesome:</p> <pre><code>import scalaz._ import Scalaz._ def even(x: Int) : Validation[NonEmptyList[String], Int] = if (x % 2 ==0) x.success else "not even: %d".format(x).wrapNel.fail println( even(3) &lt;|*|&gt; even(5) ) //prints: Failure(NonEmptyList(not even: 3, not even: 5)) </code></pre> <p>I was trying to understand what the <code>&lt;|*|&gt;</code> method was doing, here is the source code:</p> <pre><code>def &lt;|*|&gt;[B](b: M[B])(implicit t: Functor[M], a: Apply[M]): M[(A, B)] = &lt;**&gt;(b, (_: A, _: B)) </code></pre> <p>OK, that is fairly confusing (!) - but it references the <code>&lt;**&gt;</code> method, which is declared thus:</p> <pre><code>def &lt;**&gt;[B, C](b: M[B], z: (A, B) =&gt; C)(implicit t: Functor[M], a: Apply[M]): M[C] = a(t.fmap(value, z.curried), b) </code></pre> <p>So I have a few questions:</p> <ol> <li>How come the method appears to take a <em>higher-kinded type</em> of one type parameter (<code>M[B]</code>) but can get passed a <code>Validation</code> (which has two type paremeters)?</li> <li>The syntax <code>(_: A, _: B)</code> defines the function <code>(A, B) =&gt; Pair[A,B]</code> which the 2nd method expects: <em>what is happening to the Tuple2/Pair in the failure case? There's no tuple in sight!</em></li> </ol>
 

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