【发布时间】:2016-03-31 10:23:02
【问题描述】:
如何申请实现以下功能?
def wrapIntoOption(state: State[S, A]): State[Option[S], Option[A]]
大局是这样的:
case class Engine(cylinders: Int)
case class Car(engineOpt: Option[Engine])
val engineOptLens = Lens.lensu((c, e) => c.copy(engineOpt = e), _.engineOpt)
def setEngineCylinders(count: Int): State[Engine, Int] = State { engine =>
(engine.copy(cylinders = count), engine.cylinders)
}
def setEngineOptCylinders(count: Int): State[Option[Engine], Option[Int]] = {
??? // how to reuse setEngineCylinders?
}
def setCarCylinders(count: Int): State[Car, Option[Int]] = {
engineOptLens.lifts(setEngineOptCylinders)
}
有没有更好的方法来处理 Option 属性?
【问题讨论】:
标签: scala state monads scalaz lenses