【发布时间】:2015-11-29 23:07:44
【问题描述】:
我正在尝试将 Reader monad 与 State monad 结合起来。
这是我的代码:
object StatePoc extends App {
def startsWithState : Reader[String, State[Seq[String], Unit]] = Reader { s => State { ss => (ss.filter(_.startsWith(s)), Unit)}}
def endsWithState : Reader[String, State[Seq[String], Unit]] = Reader { s => State { ss => (ss.filter(_.endsWith(s)), Unit)}}
def process: Kleisli[Id.Id, String, State[Seq[String], Unit]] = {
for {
s <- startsWithState
e <- endsWithState
} yield e
}
val all = Seq("AB", "BA", "ABA", "ABBA")
println(process("A")(all))
}
不幸的是,这段代码没有返回预期的结果:List(BA, ABA, ABBA) 而不是List(ABA, ABBA)。
【问题讨论】: