Question Scalaz Kleisli

There is a trait in scalaz called Kleisli . Take a look at the code:

 import scalaz._ import Scalaz._ type StringPair = (String, String) val f: Int => List[String] = (i: Int) => List((i |+| 1).toString, (i |+| 2).toString) val g: String => List[StringPair] = (s: String) => List("X" -> s, s -> "Y") val k = kleisli(f) >=> kleisli(g) //this gives me a function: Int => List[(String, String)] 

Calling the function k with a value of 2 gives:

 println( k(2) ) //Prints: List((X,3), (3,Y), (X,4), (4,Y)) 

My question is: how to use Scalaz to combine f and g to get the function m, so the output of m (2) will be as follows:

 val m = //??? some combination of f and g println( m(2) ) //Prints: List((X,3), (X,4), (3,Y), (4,Y)) 

Is it possible?

+7
scala functional-programming scalaz
source share
1 answer

Looks like you want to transpose . Scalaz does not give this, but it should be easy to write. The m function you want is val m = f andThen (_.map(g)) andThen transpose andThen (_.join)

+3
source share

All Articles