这是我之前question
的后续内容我们可以定义一个函数,它按路径(List[String], XmlNode) => Option[XmlNode]
查找XML节点,作为按名称获取子节点的函数(String, XmlNode) => Option[XmlNode]
的组合。
我们使用A => M[A]
函数M
,其中monoid
是一个monad,形成{{1}},因此我们可以轻松地将它们组合起来。
现在我想知道是否还有其他有趣的例子来组合这些功能。
答案 0 :(得分:6)
骑士学习Scalaz的任务示例
http://eed3si9n.com/learning-scalaz/A-knights-quest.html
前
def in3: List[KnightPos] =
for {
first <- move
second <- first.move
third <- second.move
} yield third
def canReachIn3(end: KnightPos): Boolean = in3 contains end
之后(使用scalaz.Endomorphic)
val moveK: Kleisli[List, KnightPos, KnightPos] = Kleisli(_.move)
def in(n: Int): List[KnightPos] =
moveK.endo.multiply(n).run.run(this)
def canReachIn(n: Int, end: KnightPos): Boolean = in(n) contains end
https://gist.github.com/xuwei-k/c77aa4e19c0b4d4c10e2/revisions