Introduction to optics: lenses and prisms

gcanti
gcanti
Dec 8, 2016 · 4 min read

The problem

type Street = { num: number, name: string };
type Address = { city: string, street: Street };
const a1: Address = 
{ city: 'london', street: { num: 23, name: 'high street' } }
const name = a1.street.name
const a2: Address = {
...a1,
street: {
...a1.street,
name: 'main street'
}
}

Lenses

interface Lens<S, A> {
get(s: S): A,
set(a: A, s: S): S
}
const address: Lens<Address, Street> = {
get: address => address.street,
set: (street, address) => ({ ...address, street })
}
address.get(a1)
// => {num: 23, name: "high street"}
address.set({num: 23, name: 'main street'}, a1)
// => {city: "london", street: {num: 23, name: "main street"}}
const street: Lens<Street, string> = {
get: street => street.name,
set: (name, street) => ({ ...street, name })
}

Composition

function composeLens<A, B, C>(ab: Lens<A, B>, bc: Lens<B, C>): Lens<A, C> {

return {
get: a => bc.get(ab.get(a)),
set: (c, a) => ab.set(bc.set(c, ab.get(a)), a)
}
}
const streetName = composeLens(address, street)streetName.get(a1)
// => "high street"
streetName.set('main street', a1)
// => {city: "london", street: {num: 23, name: "main street"}}

Modify

function overLens<S, A>(lens: Lens<S, A>, f: (a: A) => A, s: S): S {
return lens.set(f(lens.get(s)), s)
}
function capitalize(s: string): string {
return s.substring(0, 1).toUpperCase() + s.substring(1)
}
overLens(streetName, capitalize, a1)
// => {city: "london", street: {num: 23, name: "High street"}}

Prisms

interface Prism<S, A> {
get(s: S): ?A,
set(a: A, s: S): S
}
const first: Prism<string, string> = {
get: s => s ? s.substring(0, 1) : null,
set: (a, s) => s.length ? a + s.substring(1) : ''
}
function composePrism<A, B, C>(ab: Prism<A, B>, bc: Prism<B, C>): Prism<A, C> {  return {
get: a => {
const b = ab.get(a)
return b == null ? null : bc.get(b)
},
set: (c, a) => {
const b = ab.get(a)
return b == null ? a : ab.set(bc.set(c, b), a)
}
}
}
function overPrism<S, A>
(prism: Prism<S, A>, f: (a: A) => A, s: S): S {
const a = prism.get(s)
return a ? prism.set(f(a), s) : s
}
function toUpper(s: string): string {
return s.toUpperCase()
}
overPrism(composePrism(streetName, first), toUpper, a1)
// => {city: "london", street: {num: 23, name: "High street"}}

Union types

type Domicile
= { type: 'office', address: Address }
| { type: 'personal', address: string };
const office: Prism<Domicile, Address> = {
get: d => d.type === 'office' ? d.address : null,
set: (address, d) => d.type === 'office' ?
{ type: 'office', address } : d
}
const d1 = { type: 'office', address: a1 }
const d2 = { type: 'personal', address: '23 high street' }
office.get(d1)
// => {city: "london", street: {num: 23, name: "high street"}}
office.get(d2)
// => null

gcanti

Written by

gcanti

mathematician and rock climber

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade