Function syntax puzzler in scalaz

Posted by oxbow_lakes on Stack Overflow See other posts from Stack Overflow or by oxbow_lakes
Published on 2010-03-30T12:51:00Z Indexed on 2010/03/30 12:53 UTC
Read the original article Hit count: 438

Following watching Nick Partidge's presentation on deriving scalaz, I got to looking at this example, which is just awesome:

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) <|*|> even(5) ) //prints: Failure(NonEmptyList(not even: 3, not even: 5))

I was trying to understand what the <|*|> method was doing, here is the source code:

def <|*|>[B](b: M[B])(implicit t: Functor[M], a: Apply[M]): M[(A, B)] 
    = <**>(b, (_: A, _: B))

OK, that is fairly confusing (!) - but it references the <**> method, which is declared thus:

def <**>[B, C](b: M[B], z: (A, B) => C)(implicit t: Functor[M], a: Apply[M]): M[C] 
    = a(t.fmap(value, z.curried), b)

So I have a few questions:

  1. How come the method appears to take a monad of one type parameter (M[B]) but can get passed a Validation (which has two type paremeters)?
  2. How does the syntax (_: A, _: B) define the function (A, B) => C which the 2nd method expects? It doesn't even define an output via =>

© Stack Overflow or respective owner

Related posts about scala-2.8

Related posts about scalaz