4

Sorry for the poorly worded title, but I don't even know how to ask it properly.

How can I turn this?

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

into this

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

When I try that I get the following error:

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’
Rein Henrichs
  • 15,437
  • 1
  • 45
  • 55
Joe Hillenbrand
  • 845
  • 9
  • 26

1 Answers1

5

Turns out I just have to replace the ^? with . and change Lens' to Traversal'

instPublicIP' :: Traversal' Instance (Maybe Text)
instPublicIP' = insNetworkInterfaces . ix 0 . iniAssociation . _Just . iniaPublicIP
Joe Hillenbrand
  • 845
  • 9
  • 26