Skip to content

Latest commit

 

History

History
46 lines (38 loc) · 5.03 KB

File metadata and controls

46 lines (38 loc) · 5.03 KB

Lenses – Laparoscopic Surgery for your Objects

This workshop will focus on practical problems that are elegantly solved using Lenses. The workshop uses Scala and shows practical examples of how to make a Lens.
You will learn how to make a Lens yourself (10 lines of Scala code) and will build several kinds of Lenses. This workshop purposely shies away from using a 3rd-party Lens library in order to facilitate a deeper understanding of how Lenses work.

This workshop is not designed to be completed in the time we have here. Rather today we will complete the first module and then you should complete the other modules at your own pace, either at this conference or otherwise.

This course consists of six modules which you should master in the following order:

Course Modules

  1. Lenses - A Lens is an optic that lets you descend deeply into a Product structure (e.g. Tuple, Case class, HList, Map) and replace part of it in a pure way.
  2. Isomorphisms(Iso) - An Iso is an optic which converts elements of type S into elements of type A without loss
  3. Prisms - A Prism is an optic that you can use to select a piece of a sum (co-product) type. Examples from Scala include a sealed trait, a Try, an Either, an Enum
  4. Traversals - A Traversal is a type of optic that has 0 to n targets, allowing you to focus from a type S into 0 to n values of type A.
  5. Folds - Traversables are also Folds. In combination with a Monoid they allow you to crush/reduce ... the elements in a traversal
  6. Case Study - This case study is an abstract filtering language. The requirement driving its design is that there is a data structure, a Tweet, that resides in many places, in a relational database, in a stream, in Cassandra, in an Elastic Search index, etc. And users want to be able to filter out Tweets using the same query language regardless of their source. This case study is smaller version of a production project in my paying job. What is noteworthy about the abstractions here is that it only took 2 functions and about 100 lines of code to build an implementation of the filter language compiler that produced MongoDB queries: a 3 line Monoid crush function and the actual function that transforms the Abstract Filtering ADT to MongoDB queries that are composed into a single query expression.
    The Applicative Functor and Functor make this possible. That's why its so important that you understand them.

After today's session on Lenses you can begin anywhere. But try and gain some mastery of the material that precedes, especially the Functor and Applicative Functor.
These two ideas from Category Theory are what gives the various abstractions in the Monocle Optics library their composability. A Prism composes with a Traversal with an Iso with a Lens ad infinitum. In my experience of the past 30 years reusability has been mostly an illusion until now.

The exercises are in the form of partially completed code. The actual implementation of some of the code is missing but is in the answers branch if you get stuck for longer than you can stand. But remember that if you go that route you will fail the exam;)

Prerequisites

  1. JDK 8
  2. Git
  3. SBT
  4. Working knowledge of Scala
  5. Knowledge of the Principals of Functional Programming is helpful, in particular Applicative Functors, Functors, and Abstract Data Types is essential to understanding Traversals and Folds.
  6. There are several papers and videos that you should study:
    1. The Essence of the Iterator Pattern
    2. Daniela Sfregola - A Pragmatic Introduction to Category Theory
    3. Edward Kmett provides the basis of the Monocle Lens library.
    4. Bartosz Milewski - A Crash Course in Category Theory