Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compose function to applicatives with scalaz

While learning Scalaz 6, I'm trying to write type-safe readers returning validations. Here are my new types:

type ValidReader[S,X] = (S) => Validation[NonEmptyList[String],X]
type MapReader[X] = ValidReader[Map[String,String],X]

and I have two functions creating map-readers for ints and strings (*):

def readInt( k: String ): MapReader[Int] = ...
def readString( k: String ): MapReader[String] = ...

Given the following map:

val data = Map( "name" -> "Paul", "age" -> "8" )

I can write two readers to retrieve the name and age:

val name = readString( "name" )
val age = readInt( "age" )

println( name(data) ) //=> Success("Paul")
println( age(data) )  //=> Success(8)

Everything works fine, but now I want to compose both readers to build a Boy instance:

case class Boy( name: String, age: Int )

My best take is:

  val boy = ( name |@| age ) {
    (n,a) => ( n |@| a ) { Boy(_,_) }
  }
  println( boy(data) ) //=> Success(Boy(Paul,8))

It works as expected, but the expression is awkward with two levels of applicative builders. Is there a way, to get the following syntax to work ?

  val boy = ( name |@| age ) { Boy(_,_) }

(*) Full and runnable implementation in: https://gist.github.com/1891147


Update: Here is the compiler error message that I get when trying the line above or Daniel suggestion:

[error] ***/MapReader.scala:114: type mismatch;
[error]  found   : scalaz.Validation[scalaz.NonEmptyList[String],String]
[error]  required: String
[error]   val boy = ( name |@| age ) { Boy(_,_) }
[error]                                    ^
like image 334
paradigmatic Avatar asked Feb 23 '12 07:02

paradigmatic


2 Answers

How about this?

val boy = (name |@| age) {
  (Boy.apply _).lift[({type V[X]=ValidationNEL[String,X]})#V]
}

or using a type alias:

type VNELStr[X] = ValidationNEL[String,X]

val boy = (name |@| age) apply (Boy(_, _)).lift[VNELStr]

This is based on the following error message at the console:

scala> name |@| age apply Boy.apply
<console>:22: error: type mismatch;
 found   : (String, Int) => MapReader.Boy
 required: (scalaz.Validation[scalaz.NonEmptyList[String],String], 
            scalaz.Validation[scalaz.NonEmptyList[String],Int]) => ?

So I just lifted Boy.apply to take the required type.

like image 172
huynhjl Avatar answered Oct 20 '22 17:10

huynhjl


Note that since Reader and Validation (with a semigroup E) are both Applicative, their composition is also Applicative. Using scalaz 7 this can be expressed as:

import scalaz.Reader
import scalaz.Reader.{apply => toReader}
import scalaz.{Validation, ValidationNEL, Applicative, Kleisli, NonEmptyList}

//type IntReader[A] = Reader[Int, A] // has some ambigous implicit resolution problem
type IntReader[A] = Kleisli[scalaz.IdInstances#Id, Int, A]
type ValNEL[A] = ValidationNEL[Throwable, A]

val app = Applicative[IntReader].compose[ValNEL]

Now we can use a single |@| operation on the composed Applicative:

val f1 = toReader((x: Int) => Validation.success[NonEmptyList[Throwable], String](x.toString))
val f2 = toReader((x: Int) => Validation.success[NonEmptyList[Throwable], String]((x+1).toString))

val f3 = app.map2(f1, f2)(_ + ":" + _)

f3.run(5) should be_==(Validation.success("5:6"))
like image 26
ron Avatar answered Oct 20 '22 16:10

ron