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

Manuel Gómez targen at gmail.com
Fri Oct 10 13:58:06 UTC 2014


On Fri, Oct 10, 2014 at 9:21 AM, Nicola Gigante
<nicola.gigante at gmail.com> wrote:
> 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).


More information about the Haskell-Cafe mailing list