gpt4 book ai didi

haskell - 如何在独立的Getter中使用(^?ix 0)?

转载 作者:行者123 更新时间:2023-12-02 12:01:12 25 4
gpt4 key购买 nike

抱歉,标题措辞不好,但我什至不知道如何正确提问。

我该如何转动它?

instPublicIP :: Instance -> Maybe Text
instPublicIP inst =
inst ^. insNetworkInterfaces ^? ix 0 . iniAssociation . _Just . iniaPublicIP . _Just

进入此

instPublicIP' :: Lens' Instance (Maybe Text)
instPublicIP' = insNetworkInterfaces ^? ix 0 . iniAssociation . _Just . iniaPublicIP . _Just

当我尝试时,出现以下错误:

Main.hs:198:3:
Couldn't match expected type ‘(Maybe Text -> f (Maybe Text))
-> Instance -> f Instance’
with actual type ‘Maybe Text’
Relevant bindings include
instPublicIP' :: (Maybe Text -> f (Maybe Text))
-> Instance -> f Instance
(bound at app/Main.hs:197:1)
In the expression:
insNetworkInterfaces
^? ix 0 . iniAssociation . _Just . iniaPublicIP . _Just
In an equation for ‘instPublicIP'’:
instPublicIP'
= insNetworkInterfaces
^? ix 0 . iniAssociation . _Just . iniaPublicIP . _Just

Main.hs:198:27:
Couldn't match type ‘InstanceNetworkInterface’
with ‘Instance -> f0 Instance’
Expected type: (InstanceNetworkInterface
-> Const (Data.Monoid.First Text) InstanceNetworkInterface)
-> (([InstanceNetworkInterface] -> f0 [InstanceNetworkInterface])
-> Instance -> f0 Instance)
-> Const
(Data.Monoid.First Text)
(([InstanceNetworkInterface] -> f0 [InstanceNetworkInterface])
-> Instance -> f0 Instance)
Actual type: (IxValue
(([InstanceNetworkInterface] -> f0 [InstanceNetworkInterface])
-> Instance -> f0 Instance)
-> Const
(Data.Monoid.First Text)
(IxValue
(([InstanceNetworkInterface] -> f0 [InstanceNetworkInterface])
-> Instance -> f0 Instance)))
-> (([InstanceNetworkInterface] -> f0 [InstanceNetworkInterface])
-> Instance -> f0 Instance)
-> Const
(Data.Monoid.First Text)
(([InstanceNetworkInterface] -> f0 [InstanceNetworkInterface])
-> Instance -> f0 Instance)
In the first argument of ‘(.)’, namely ‘ix 0’
In the second argument of ‘(^?)’, namely
‘ix 0 . iniAssociation . _Just . iniaPublicIP . _Just’

最佳答案

结果我只需要将 ^? 替换为 . 并将 Lens' 更改为 Traversal'

instPublicIP' :: Traversal' Instance (Maybe Text)
instPublicIP' = insNetworkInterfaces . ix 0 . iniAssociation . _Just . iniaPublicIP

关于haskell - 如何在独立的Getter中使用(^?ix 0)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33115595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com