在trait库中有一个名为Kleisli的黄玉。查看代码:
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)]调用值为2的函数k提供:
println( k(2) ) //Prints: List((X,3), (3,Y), (X,4), (4,Y))我的问题是:如何使用Scalaz组合f和g来得到函数m,使m(2)的输出是:
val m = //??? some combination of f and g
println( m(2) ) //Prints: List((X,3), (X,4), (3,Y), (4,Y))这有可能吗?
发布于 2010-04-01 17:52:20
看来你想要transpose。Scalaz不提供这种功能,但是编写起来应该很容易。您想要的函数m是val m = f andThen (_.map(g)) andThen transpose andThen (_.join)。
https://stackoverflow.com/questions/2559834
复制相似问题