On this page:
lens?
make-lens
lens-get
lens-set
lens-map
lens/  c
identity-lens
lens-pipe
pair.first
pair.second
entry.key
entry.value
byte.bit
8.0

1 Lenses

 (require glass/lens) package: glass

A lens is a type of optic for focusing on small parts of a subject. A lens is built from a getter function, which extracts the focus from the subject, and a setter function, which takes a subject and a replacement for the focus and builds a new subject.

procedure

(lens? v)  boolean?

  v : any/c
A predicate for lenses.

procedure

(make-lens getter setter [#:name name])  lens?

  getter : (-> any/c any/c)
  setter : (-> any/c any/c any/c)
  name : (or/c interned-symbol? #f) = #f
Constructs a lens named name that focuses on subjects by calling getter on the subject and updates the focus by calling setter with the subject and the replacement focus.

Examples:
(define-tuple-type point (x y))
(define point.x
  (make-lens point-x (λ (p x) (point x (point-y p))) #:name 'point.x))

 

> (lens-get point.x (point 4 7))

4

> (lens-set point.x (point 4 7) 100)

(point 100 7)

procedure

(lens-get lens subject)  any/c

  lens : lens?
  subject : any/c
Returns the focus of lens on subject.

procedure

(lens-set lens subject replacement)  any/c

  lens : lens?
  subject : any/c
  replacement : any/c
Updates subject with lens by replacing its focus with replacement, returning an updated subject.

procedure

(lens-map lens subject mapper)  any/c

  lens : lens?
  subject : any/c
  mapper : (-> any/c any/c)
Updates subject with lens by applying mapper to its focus, returning an updated subject with the mapped focus.

Example:
> (lens-map entry.value (entry 'a 16) sqrt)

(entry 'a 4)

procedure

(lens/c subject-contract focus-contract)  contract?

  subject-contract : contract?
  focus-contract : contract?
A contract combinator for lenses. Creates a contract that accepts lenses whose subjects are checked with subject-contract and whose foci are checked with focus-contract.

The identity lens, which focuses on the entire subject and replaces it entirely when given a new focus.

Examples:

procedure

(lens-pipe lens ...)  lens?

  lens : lens?
Joins each lens to the next, building a composed lens that focuses on subjects by recursively focusing on the subject once with each lens from left to right. If only one lens is given, it is returned unchanged, and if no lenses are given, identity-lens is returned.

Examples:
(define entry.key.first (lens-pipe entry.key pair.first))

 

> (lens-get entry.key.first (entry (pair 'a 'c) 5))

'a

> (lens-set entry.key.first (entry (pair 'a 'c) 5) 'f)

(entry (pair 'f 'c) 5)

Lenses that focus on the first and second values of a pair, respectively.

Examples:
> (lens-get pair.first (pair 4 8))

4

> (lens-set pair.second (pair 4 8) 100)

(pair 4 100)

Lenses that focus on the key and value of an entry, respectively.

Examples:
> (lens-get entry.key (entry 'a 1))

'a

> (lens-set entry.value (entry 'a 1) 5)

(entry 'a 5)

procedure

(byte.bit position)  (lens/c byte? bit?)

  position : (integer-in 0 7)
Constructs a lens that focuses on the bit at position in a byte, with bit positions numbered from left to right.

Examples:
> (lens-get (byte.bit 7) (byte 1 1 1 1 1 1 1 0))

0

> (lens-set (byte.bit 7) (byte 0 0 0 0 0 0 0 0) 1)

1