4
我正在嘗試將Reader
monad與State
monad結合使用。 這裏是我的代碼:如何結合閱讀器和狀態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)
。
感謝。使用嵌套的用於CoMP也提高 DEF過程= { 爲{ 小號< - startsWithState ë< - endsWithState }得到的{ _ < - 電子 SS < - S }收率SS } –