On this page:
hash-ref-lens
hash-ref-nested-lens
hash-pick-lens
6.2.901.900

4 Hash Lenses

procedure

(hash-ref-lens key)  lens?

  key : any/c
Constructs a lens that targets hashes and views the value of key.

Examples:

> (define foo-lens (hash-ref-lens 'foo))
> (lens-view foo-lens (hash 'foo 10 'bar 20))

10

> (lens-set foo-lens (hash 'foo 10 'bar 20) 1000)

'#hash((foo . 1000) (bar . 20))

procedure

(hash-ref-nested-lens key ...)  lens?

  key : any/c
Contructs a lens that targets hashes with nested hashes as values and views the value obtained by using each key in order.

Examples:

> (define foo-bar-lens (hash-ref-nested-lens 'foo 'bar))
> (lens-view foo-bar-lens (hash 'foo (hash 'bar 1)))

1

> (lens-set foo-bar-lens (hash 'foo (hash 'bar 1)) 1000)

'#hash((foo . #hash((bar . 1000))))

procedure

(hash-pick-lens key ...)  lens?

  key : any/c
Creates a lens that views a subset of the target hash-table with the given keys. The view, is another hash-table with only the given keys and their corrosponding values in the target hash-table.

Examples:

> (lens-view (hash-pick-lens 'a 'c) (hash 'a 1 'b 2 'c 3))

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

> (lens-set (hash-pick-lens 'a 'c) (hash 'a 1 'b 2 'c 3) (hash 'a 4 'c 5))

'#hash((a . 4) (b . 2) (c . 5))