Skip to content

monadplus/scala-lenses

Repository files navigation

The magic of composition

During this talk, we are going to dive into Monocle and unravel the mystery surrounding optics.

Slides here.

Public

This talk is aimed for developers with basic notion of:

  • Scala
  • Functional programming

The examples are in written in Scala so a basic notion of the syntax is assumed.

If you are not acquainted with the words Functor, Appicative, Foldable, Traverse I would recommend visiting first the cats and typeclassopedia and get a basic notion of these concepts.

Motivation

case class Street(number: Int, name: String)
case class Address(city: String, street: Street)
case class Company(name: String, address: Address)
case class Employee(name: String, company: Company)

val employee = Employee("john", Company("awesome inc", Address("london", Street(23, "high street"))))

Old plain scala:

employee.copy(
  company = employee.company.copy(
    address = employee.company.address.copy(
      street = employee.company.address.street.copy(
        name = employee.company.address.street.name.capitalize // luckily capitalize exists
      )
    )
  )
)

With lenses:

(company composeLens
 address composeLens
 street composeLens streetName).modify(_.capitalize)(employee)

Libraries

Lenses provides more composable versions of the abstractions you already known how to use in Scala/Haskell.

Both libraries provides rich APIs to work with lenses and a battery of utilities to make your life easier. Lenses is based on Van Laarhoven optics while Monocle uses an alternative encoding. The reason behind not using Van Laarhoven nor profunctor optics is because they are inefficient in scala and not well supported by the language (scala does not support N-rank types).

Hierarchy

Simplified Class Diagram

Monocle lenses hierarchy

Class Diagram

Lenses hierarchy in haskell

Composition

The result is their lowest upper bound in the hierarchy (or an error if that bound doesn't exist).

Getter Iso Lens Optional Prism Setter Traversal
Getter Getter Getter Getter Fold Fold - Fold
Iso Getter Iso Lens Optional Prism Setter Traversal
Lens Getter Lens Lens Optional Optional Setter Traversal
Optional Fold Optional Optional Optional Optional Setter Traversal
Prism Fold Prism Optional Optional Prism Setter Traversal
Setter - Setter Setter Setter Setter Setter Setter
Traversal Fold Traversal Traversal Traversal Traversal Setter Traversal

Credits: