7
有一个trait
叫做Kleisli在scalaz库。看代码:Scalaz Kleisli question
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)]
调用函数k
与2的值给出:
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))
这甚至可能?
我现在无法尝试此操作,但是我们不能使用MA#序列执行换位吗? http://scalaz.googlecode.com/svn/continuous/latest/browse.sxr/scalaz/example/ExampleTraverse.scala.html – retronym 2010-04-02 08:19:38
你是对的。你完全可以,给予流的“zippy”Applicative实例(或ZipStream)。 – Apocalisp 2010-04-02 17:40:30
糟糕,不,它没有正确的行为。 – Apocalisp 2010-04-02 18:00:39