Scala 通过“镜头”映射“状态”`

Scala 通过“镜头”映射“状态”`,scala,scalaz,state-monad,lenses,monocle-scala,Scala,Scalaz,State Monad,Lenses,Monocle Scala,有签名功能吗 lensMapState[S, T, A](lens : Lens[S, T]): State[T, A] => State[S, A] 使用语义运行所选零件的修改并获得结果 一种实现可能是 def lensMapState[S, T, A](lens: Lens[S, T]): State[T, A] => State[S, A] = stateT => State { s => val (result, x) = stateT.ru

有签名功能吗

lensMapState[S, T, A](lens : Lens[S, T]): State[T, A] => State[S, A]
使用语义运行所选零件的修改并获得结果

一种实现可能是

def lensMapState[S, T, A](lens: Lens[S, T]): State[T, A] => State[S, A] =
    stateT => State { s =>
      val (result, x) = stateT.run(lens.get(s))
      (lens.set(result)(s), x)
    } 

但是如果有更直接的方法使用or?

我想你想要的是这样的:

import scalaz._
import Scalaz._

case class Person(name: String, age: Int)
case object Person {
  val _age = Lens.lensu[Person, Int]((p, a) => p.copy(age = a), _.age 
}

val state = for {
  a <- Person._age %= { _ + 1 } 
} yield a

state.run(Person("Holmes", 42))
中定义了许多与镜头/状态相关的函数
单片眼镜遵循类似的原理。据我所知,相关功能是在monocle.state中定义的。

不太清楚。我需要一个从
State[Int,a]
State[Person,a]
的映射,
%=
是从
Int->Int
State[Person,Int]
的转换。啊,对不起,我没有对你的类型给予足够的关注。但我仍然认为这可能会导致一些问题,所以+1
res0: scalaz.Id.Id[(Person, Int)] = (Person(Holmes,43),43)