当输入值(未设置字段)为?时,是否有任何方法可以应用Control-Lens-Setter.html#g:4中提到的组合器/设置器?Just x
例如,考虑(~+)
组合器,如果我具有以下条件:
let target = (1, 2) input1 = Just 10 input2 = Nothing
我想要一个执行以下操作的设置器:
(11, 2) == target & (_1 . someSetter) +~ input1 & (_2 . someSetter) +~ input2
我尝试避免使用fmap
或maybe/fromMaybe
在这种情况下使用,因为我有很多此类操作要做,并且我更喜欢通过利用镜片的简洁/简洁来避免样板。
您总是可以定义自己的设置器。
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
那是关于镜头的很棒的事情。它们只是常规功能。