Glass: Composable Optics
(require glass) | package: glass |
This is an experimental library for optics. An optic is an object for viewing one form of data, called the subject, in some other form, called a focus. Optics are bidirectional: in addition to viewing the focus, optics allow changing the focus to get a new, updated subject. There are several different kinds of optics, each of which expresses a different relationship between subject and focus:
Lenses focus on one small part of the subject, such as a field of a struct. Other parts of the subject are left untouched when the focus is changed. For example, the entry.key lens focuses on the key of an entry object, allowing you to change the key of an entry.
Prisms focus on one specific kind of subject, such as a subtype of a struct. Other kinds of subjects are ignored. For example, success-prism focuses on successful result objects and ignores failed ones, allowing you to change the values inside only successful results.
Traversals focus on several parts of the subject at once, such as the characters of a string. Each focus can be updated to a new value, but a traversal cannot change the total number of foci.
I’m working on this library for fun as a hypothetical successor to the lens library. I might put more work into it, or I might not. Absolutely no promise of backwards compatibility whatsoever. Caveat emptor.