Lenses for Generic Collections
ref-lens
nth-lens
map-lens
first-lens
rest-lens
take-lens
drop-lens
subsequence-lens
subsequence*-lens
8.12

Lenses for Generic Collections🔗ℹ

 (require data/collection/lens) package: collections-lens

This provides lenses that examine and modify generic collections.

procedure

(ref-lens key)  lens?

  key : any/c
(ref-lens key default)  lens?
  key : any/c
  default : any/c
Constructs a lens for a keyed value of an indexable? instance.

Examples:
> (lens-transform (ref-lens 'key)
                  (hash 'key 2)
                  (λ (x) (* 10 x)))

'#hash((key . 20))

> (lens-view (ref-lens 'key 'default)
             (hash))

'default

procedure

(nth-lens index)  lens?

  index : exact-nonnegative-integer?
Constructs a lens for a single element of a sequence?.

Example:
> (lens-transform (nth-lens 1)
                  '(1 2 3)
                  (λ (x) (* 10 x)))

'(1 20 3)

procedure

(map-lens lens)  lens?

  lens : lens?
Constructs a lens that maps over sequence? instances.

Examples:
> (lens-view (map-lens (ref-lens 'key))
             (list (hash 'key 1 'a 2) (hash 'key 2 'b 3)))

#<stream>

> (sequence->list
   (lens-view (map-lens (ref-lens 'key))
              (list (hash 'key 1 'a 2) (hash 'key 2 'b 3))))

'(1 2)

> (sequence->list
   (lens-set (map-lens (ref-lens 'key))
             (list (hash 'key 1 'a 2) (hash 'key 2 'b 3))
             (list 100 200)))

'(#hash((a . 2) (key . 100)) #hash((b . 3) (key . 200)))

> (sequence->list
   (lens-transform (map-lens (ref-lens 'key))
                   (list (hash 'key 1 'a 2) (hash 'key 2 'b 3))
                   (λ (lst) (map (λ (x) (* 10 x)) lst))))

'(#hash((a . 2) (key . 10)) #hash((b . 3) (key . 20)))

value

first-lens : lens?

A lens for the first element of a sequence?.

value

rest-lens : lens?

A lens for all but the first element of a sequence?.

procedure

(take-lens n)  lens?

  n : exact-nonnegative-integer?
Constructs a lens for the first n elements of a sequence?.

procedure

(drop-lens n)  lens?

  n : exact-nonnegative-integer?
Constructs a lens for the rest of a sequence? after the first n elements.

procedure

(subsequence-lens start end)  lens?

  start : exact-nonnegative-integer?
  end : exact-nonnegative-integer?
A lens for a subsequence from start to end. The range behavior corresponds to subsequence.

procedure

(subsequence*-lens start len)  lens?

  start : exact-nonnegative-integer?
  len : exact-nonnegative-integer?
A lens for a subsequence starting at start with length len elements. The range behavior corresponds to subsequence*.