仅在输入值为“ Just x”时才套用镜头“ setter”吗?

时间:2019-09-26 05:37:13

标签: haskell lens lenses

输入值(正在设置的字段不是)为{{时,有什么方法可以应用Control-Lens-Setter.html#g:4中提到的组合器/设置器1}}?

例如,如果我具有以下条件,请考虑使用Just x组合器:

(~+)

我想要一个执行以下操作的二传手:

let target = (1, 2)
    input1 = Just 10
    input2 = Nothing

在这种情况下,我尝试避免使用(11, 2) == target & (_1 . someSetter) +~ input1 & (_2 . someSetter) +~ input2 fmap,因为我要做很多这样的操作,并且宁愿通过利用镜头的简洁/简洁来避免样板

1 个答案:

答案 0 :(得分:5)

您总是可以定义自己的设置器。

maybeSetter :: (b -> a -> a) -> ASetter s t a a -> Maybe b -> s -> t
maybeSetter g f x = runIdentity . f (Identity . maybe id g x)

(+!) :: Num a => ASetter s t a a -> Maybe a -> s -> t
(+!) = maybeSetter (+)

let target = (1, 2)
    input1 = Just 10
    input2 = Nothing
in
    (11, 2) == target & (_1 . someSetter) +! input1 
                      & (_2 . someSetter) +! input2

您还可以轻松定义其他设置器。

(-!) :: Num a => ASetter s t a a -> Maybe a -> s -> t
(-!) = maybeSetter subtract

(||!) :: ASetter s t Bool Bool -> Maybe Bool -> s -> t
(||!) = maybeSetter (||)

(<>!) :: Semigroup a => ASetter s t a a -> Maybe a -> s -> t
(<>!) = maybeSetter (flip (<>))

(.!) :: ASetter s t a a -> Maybe a -> s -> t
(.!) = maybeSetter const

(%!) :: ASetter s t a a -> Maybe (a -> a) -> s -> t
(%!) = maybeSetter id

那是镜头的妙处。它们只是常规功能。