Skip to content

Instantly share code, notes, and snippets.

@wigahluk
Last active October 29, 2021 13:26
Show Gist options
  • Save wigahluk/d35e4a3a0474c3355bb1cfa1a8e1848a to your computer and use it in GitHub Desktop.
Save wigahluk/d35e4a3a0474c3355bb1cfa1a8e1848a to your computer and use it in GitHub Desktop.
Optics References

Optics, Lenses, Profunctors and related beasts

I got interested in lenses because I was looking for a better way to handle state in front end applications (ELM Architecture and Redux were nice ideas but using Elm or PureScript face some management obstacles and Redux lacks types and more explicit composition). Studying them I found profunctors and the more I knew about them both the more interested I was. Now I look at optics as profunctor transformations that can be applied to a much wider menu of use cases than data accessors for nested structures.

This Gist is my collection of shortcuts to several resources here and there that either I consult frequently or I recommend frequently.

Profunctor presentation

Optics as profunctor transformations are in correspondence with the profunctorial restrictions and have the following type definition:

type Optical p s t a b = p a b -> p s t
Restriction Optic
Profunctor Iso
Strong/Cartesian Lens
Choice/Co-Cartesian Prism
Co-Choice Coprism
(Strong, Choice)/Wander AffineTraversal

Van Laarhoven presentation

Lenses are commonly represented with co-algebras, this representation is also known as Van Laarhoven and was introduced in Van Laarhoven [2009]

type Lens a b = forall f. Functor f => (b -> f b) -> (a -> f a)

My Own Stuff

References

Librarires

Haskell

PureScript

Scala

  • Monocle This library offers optics in their simple version: a -> s instead of its regular profunctor signature p a b -> p s t. They actually have the full version but seems it's only used internally.

JavaScript

  • Ramda Although not only an optics library but a functional toolkit for JS.

TypeScript

  • Lens.ts
  • Telescope.js Although not an optics library, includes a simple lens implementation used in a state management context. (Disclosure: I co-authored this library)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment