在这个关于镜头好文章加布里埃尔·冈萨雷斯写这样的代码:
import Control.Monad.Trans.Class
import Control.Monad.Trans.State
strike :: StateT Game IO ()
strike = do
lift $ putStrLn "*shink*"
boss.health -= 10
Run Code Online (Sandbox Code Playgroud)
但后来写道
newState^.boss.health
Run Code Online (Sandbox Code Playgroud)
我知道为了修改State
monad(这里是StateT
monad变换器)你需要使用get
函数获取状态,用它做一些事情,然后使用该put
函数.但是这里作者没有使用这些功能.同样在第二个例子中,他使用^
操作符,这意味着使用boss
镜头需要一个Game
值.但他在第一个例子中没有这样做.怎么样?
我们来看看这条线boss.health -= 10
.-=
被限定在所述lens
包为:
(-=) :: (MonadState s m, Num a) => ASetter' s a -> a -> m ()
l -= b = State.modify (l -~ b)
Run Code Online (Sandbox Code Playgroud)
关于这个定义的两点:
它不使用get
/ put
,但是modify
,modify f
相当于get x >>= \x -> put (f x)
第一个论点是ASetter' s a
.镜片,特别是boss.health
该物品使用的镜片,起作用.操作员说"我将使用您通过的镜头从状态值中减去".
归档时间: |
|
查看次数: |
89 次 |
最近记录: |