给定一些定义镜头的数据结构,例如:
import Control.Lens
data Thing =
Thing {
_a :: String
, _b :: String
, _c :: Int
, _d :: Int
}
makeLenses ''Thing
并给出了一些我想使用多个getter调用的函数,例如:
fun :: Int -> String -> Int -> String -> Bool
fun = undefined
此刻,我在访问每个字段时都遇到了很多麻烦,例如:
thing = Thing "hello" "there" 5 1
answer = fun (thing^.c) (thing^.a) (thing^.d) (thing^.b)
考虑到lens
库在大多数其他情况下的简洁性,我希望可以得到一些更优雅的东西,但是我找不到任何可以帮助这种特定情况的组合器。
由于在观看或设置“模式”中都可以使用任何镜头,因此我们至少需要view X
为每个镜头X指定。但是,对于任何镜头l :: Lens' a b
,view l
其类型都类似于a -> b
您转换某些MonadReader
杂色。
因此,我们可以使用的Applicative
实例来消除某些重复((->) a)
。
thing & fun <$> view c <*> view a <*> view d <*> view b
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句