Chaining a map over lists with lenses

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

On Fri, Oct 10, 2014 at 9:21 AM, Nicola Gigante
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?
`traverse` should do the trick. A traversal is like a lens, except it isn’t constrained to exactly one target. A prism is a traversal constrained to having exactly zero or one targets. `traverse` from `Data.Traversable` targets everything “in” the value. If you use `(^.)` with a traversal, though, that’ll require an instance of `Monoid` and it will in essence apply `mconcat` to the list of results. If you want them separately, try `(^..)` (pronounced `toListOf`, I believe).

Hi Nicola, there's 'traversed' to access every entry of a traversable structure, e.g. appending a "X" at the end of each 'otherField': t & myField . traversed . otherField %~ (++ "X") And there's '^..' to return a list of each entry of a traversable structure: t ^.. myField . traversed . otherField Greetings, Daniel

Il giorno 10/ott/2014, alle ore 16:36, Daniel Trstenjak
Hi Nicola,
there's 'traversed' to access every entry of a traversable structure, e.g. appending a "X" at the end of each 'otherField':
t & myField . traversed . otherField %~ (++ "X")
And there's '^..' to return a list of each entry of a traversable structure:
t ^.. myField . traversed . otherField
Thank you for both the answers! So my error was to presume that everything can be done with (^.) (which is view, if I’m correct). So I have to understand now, what is the & operator?
Greetings, Daniel
Thank you, Nicola

The & is the same as $ with arguments inverted. So instead of
t & myField . traversed . otherField %~ (++ "X")
you could write
myField . traversed . otherField %~ (++ "X") $ t
(if I recall correctly...)
Michal
On Fri, Oct 10, 2014 at 11:03 AM, Nicola Gigante
Il giorno 10/ott/2014, alle ore 16:36, Daniel Trstenjak < daniel.trstenjak@gmail.com> ha scritto:
Hi Nicola,
there's 'traversed' to access every entry of a traversable structure, e.g. appending a "X" at the end of each 'otherField':
t & myField . traversed . otherField %~ (++ "X")
And there's '^..' to return a list of each entry of a traversable
structure:
t ^.. myField . traversed . otherField
Thank you for both the answers!
So my error was to presume that everything can be done with (^.) (which is view, if I’m correct).
So I have to understand now, what is the & operator?
Greetings, Daniel
Thank you, Nicola _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (4)
-
Daniel Trstenjak
-
Manuel Gómez
-
Michal Antkiewicz
-
Nicola Gigante