[Haskell-cafe] Chaining a map over lists with lenses

Nicola Gigante nicola.gigante at gmail.com
Fri Oct 10 13:51:07 UTC 2014


Hi!

I’m sure the answer to this question is under my eyes but
I can’t understand it only from the docs.

Suppose a data type like this:

data Type1 = Type1 {
   _myField :: [Type2]
}
makeLenses ‘’Type1

data Type2 = Type2 {
   _otherField :: String
}

Then with t^.myField I get access to the [Type2] list.
Now, i would like to go through each element of the list,
apply the otherField lens to each element, and get the list
of the results.

Of course this means:

map (view otherField) (t^.myField)

but I’m sure there’s some combinator to do the same thing 
by chaining lenses/prisms/traversals/whatever, right?
E.g. something like

t^.myField.something.otherField

I’m still trying to understand the full picture about lenses,
but afaik what I need is a prism, because a list is a sum type,
right?

Thank you for your help,
Nicola




More information about the Haskell-Cafe mailing list