Haskell 使用Control.Lens中的几个getter调用函数的干净方法是什么。
给定一些定义了镜头的数据结构,例如:Haskell 使用Control.Lens中的几个getter调用函数的干净方法是什么。,haskell,haskell-lens,Haskell,Haskell Lens,给定一些定义了镜头的数据结构,例如: import Control.Lens data Thing = Thing { _a :: String , _b :: String , _c :: Int , _d :: Int } makeLenses ''Thing fun :: Int -> String -> Int -> String -> Bool fun = undefined thing = Thing "hello" "th
import Control.Lens
data Thing =
Thing {
_a :: String
, _b :: String
, _c :: Int
, _d :: Int
}
makeLenses ''Thing
fun :: Int -> String -> Int -> String -> Bool
fun = undefined
thing = Thing "hello" "there" 5 1
answer = fun (thing^.c) (thing^.a) (thing^.d) (thing^.b)
给出了一些我想用几个getter调用的函数,例如:
import Control.Lens
data Thing =
Thing {
_a :: String
, _b :: String
, _c :: Int
, _d :: Int
}
makeLenses ''Thing
fun :: Int -> String -> Int -> String -> Bool
fun = undefined
thing = Thing "hello" "there" 5 1
answer = fun (thing^.c) (thing^.a) (thing^.d) (thing^.b)
目前,我在帕伦斯访问每个领域时遇到了很多麻烦,例如:
import Control.Lens
data Thing =
Thing {
_a :: String
, _b :: String
, _c :: Int
, _d :: Int
}
makeLenses ''Thing
fun :: Int -> String -> Int -> String -> Bool
fun = undefined
thing = Thing "hello" "there" 5 1
answer = fun (thing^.c) (thing^.a) (thing^.d) (thing^.b)
考虑到
镜头库在大多数其他情况下的简洁性,我希望能有一些更优雅的东西,但我找不到任何有助于这种特殊情况的组合器。因为任何镜头都可以在查看或设置“模式”中使用,我们至少需要为每个镜头X指定视图X
。但是对于任何镜头l::lens'a b
,视图l
的类型类似于a->b
,如果你翻译一些噪音
因此,我们可以使用((->)a)
的应用程序
实例来消除一些重复
thing&fun视图c视图a视图d视图b
对于类似的内容,最好使用记录通配符
。然后您可以编写answer(Thing{..})=funcadb
通过使用(>)
应用程序实例,您可以获得非常小的优势。类似于事物和乐趣视图c视图a视图d视图b
。这可以说是更直接地表达了这个意图?@ChrisDueck,谢谢,但是镜头并不总是直接等同于记录。如果可能的话,这绝对是一个很好的提示。@tel这正是我想要的。非常感谢。