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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access