Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to compose function to applicatives with scalaz
    primarykey
    data
    text
    <p>While learning <strong>Scalaz 6</strong>, I'm trying to write type-safe readers returning validations. Here are my new types:</p> <pre><code>type ValidReader[S,X] = (S) =&gt; Validation[NonEmptyList[String],X] type MapReader[X] = ValidReader[Map[String,String],X] </code></pre> <p>and I have two functions creating map-readers for ints and strings (*):</p> <pre><code>def readInt( k: String ): MapReader[Int] = ... def readString( k: String ): MapReader[String] = ... </code></pre> <p>Given the following map:</p> <pre><code>val data = Map( "name" -&gt; "Paul", "age" -&gt; "8" ) </code></pre> <p>I can write two readers to retrieve the name and age:</p> <pre><code>val name = readString( "name" ) val age = readInt( "age" ) println( name(data) ) //=&gt; Success("Paul") println( age(data) ) //=&gt; Success(8) </code></pre> <p>Everything works fine, but now I want to compose both readers to build a <code>Boy</code> instance:</p> <pre><code>case class Boy( name: String, age: Int ) </code></pre> <p>My best take is:</p> <pre><code> val boy = ( name |@| age ) { (n,a) =&gt; ( n |@| a ) { Boy(_,_) } } println( boy(data) ) //=&gt; Success(Boy(Paul,8)) </code></pre> <p>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 ?</p> <pre><code> val boy = ( name |@| age ) { Boy(_,_) } </code></pre> <p>(*) Full and runnable implementation in: <a href="https://gist.github.com/1891147" rel="noreferrer">https://gist.github.com/1891147</a></p> <hr> <p><strong>Update:</strong> Here is the compiler error message that I get when trying the line above or Daniel suggestion:</p> <pre><code>[error] ***/MapReader.scala:114: type mismatch; [error] found : scalaz.Validation[scalaz.NonEmptyList[String],String] [error] required: String [error] val boy = ( name |@| age ) { Boy(_,_) } [error] ^ </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.
 

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