O'Reilly logo

Haskell Cookbook by Yogesh Sajanikar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

We have looked at lens in the preceding recipe. A lens applies to a field in a data structure. If we have a Traversable field type somewhere, then we can use traverse to browse over the collection. An important fact must be remembered, if we view the traverse, then the target field must be an instance of Monoid. For example, toListOf traverse exchanges will be successful, and will fetch same list of exchanges back but view traverse exchanges will fail with a complaint that Exchange is not an instance of Monoid. This happens because, traversal tries to summarize the target values by assuming that the target type is an instance of Monoid. Hence it starts with an empty value (mempty) and then starts appending values mappend/mconcat ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required