On this page:
lens-compose
identity-lens
lens-thrush
lens-join/  list
lens-join/  hash
lens-join/  vector
lens-join/  string
6.2.901.900

2 Joining and Composing Lenses

procedure

(lens-compose lens ...)  lens?

  lens : lens?
Composes the given lenses together into one compound lens. The compound lens operates similarly to composed functions do in that the last lens is the first lens the compound lens’s target is viewed through. Each successive lens "zooms in" to a more detailed view. When called with no arguments, lens-compose produces the identity lens.

Examples:

> (define first-of-second-lens (lens-compose first-lens second-lens))
> (lens-view first-of-second-lens '((1 a) (2 b) (3 c)))

2

> (lens-set first-of-second-lens '((1 a) (2 b) (3 c)) 200)

'((1 a) (200 b) (3 c))

The identity lens. Performs no destructuring at all - it’s view is the target itself. For all lenses, both (lens-compose lens identity-lens) and (lens-compose identity-lens lens) are equivalent to lens.

Examples:

> (lens-view identity-lens 4)

4

> (lens-set identity-lens 4 'a)

'a

procedure

(lens-thrush lens ...)  lens?

  lens : lens?
Like lens-compose, but each lens is combined in the opposite order. That is, the first lens is the first lens that the compound lens’s target is viewed through.

Examples:

> (define first-of-second-lens (lens-thrush second-lens first-lens))
> (lens-view first-of-second-lens '((1 a) (2 b) (3 c)))

2

> (lens-set first-of-second-lens '((1 a) (2 b) (3 c)) 200)

'((1 a) (200 b) (3 c))

procedure

(lens-join/list lens ...)  lens?

  lens : lens?
Constructs a lens that combines the view of each lens into a list of views. This lens can be used to view and set a list of values in a single target. If any of the lenses share views, then when setting the later lenses override the earlier ones.

Examples:

> (define first-third-fifth-lens
    (lens-join/list first-lens
                    third-lens
                    fifth-lens))
> (lens-view first-third-fifth-lens '(a b c d e f))

'(a c e)

> (lens-set first-third-fifth-lens '(a b c d e f) '(1 2 3))

'(1 b 2 d 3 f)

procedure

(lens-join/hash key lens ... ...)  lens?

  key : any/c
  lens : lens?
Constructs a lens that combines the view of each lens into a hash of views with keys as the hash keys. In the same manner as lens-join/list, if lenses share views later lenses take precedence when setting.

Examples:

> (define a-b-lens (lens-join/hash 'a first-lens
                                   'b third-lens))
> (lens-view a-b-lens '(1 2 3))

'#hash((a . 1) (b . 3))

> (lens-set a-b-lens '(1 2 3) (hash 'a 100 'b 200))

'(100 2 200)

procedure

(lens-join/vector lens ...)  lens?

  lens : lens?
Like lens-join/list, except the view is a vector, not a list.

Examples:

> (define vector-first-third-fifth-lens
    (lens-join/vector first-lens
                      third-lens
                      fifth-lens))
> (lens-view vector-first-third-fifth-lens '(a b c d e f))

'#(a c e)

> (lens-set vector-first-third-fifth-lens '(a b c d e f) #(1 2 3))

'(1 b 2 d 3 f)

procedure

(lens-join/string lens ...)  lens?

  lens : lens?
Like lens-join/list, except the view is a string, not a list. Each lens argument must return a char? as a view.

Examples:

> (define string-first-third-fifth-lens
    (lens-join/string first-lens
                      third-lens
                      fifth-lens))
> (lens-view string-first-third-fifth-lens '(#\a #\b #\c #\d #\e #\f))

"ace"

> (lens-set string-first-third-fifth-lens '(#\a #\b #\c #\d #\e #\f) "ACE")

'(#\A #\b #\C #\d #\E #\f)