Monocle: a small lens library
This package provides a lens implementation with minimal dependencies and a focus on ergonomics. For a more full-featured implementation see the lens package.
By convention, lens ids are prefixed by an & character.
1 Reference
(require data/monocle) | package: monocle-lib |
This module reprovides all the bindings defined in the modules documented below.
1.1 Operators
(require data/monocle/lens) | package: monocle-lib |
struct
(struct lens (getter setter) #:extra-constructor-name make-lens) getter : (procedure-arity-includes/c 1) setter : (procedure-arity-includes/c 2)
> (lens-update &car '(1 . 2) add1) '(2 . 2)
procedure
(lens-compose a b ...+) → lens?
a : lens? b : lens?
> ((lens-compose &car &cdr) '(a b c)) 'b
> ((lens-compose &car &cdr) '(a b c) 'd) '(a d c)
procedure
(lens-thread a b ...+) → lens?
a : lens? b : lens?
> ((lens-thread &cdr &car) '(a b c)) 'b
> ((lens-thread &cdr &car) '(a b c) 'd) '(a d c)
1.2 Lenses for Pairs
(require data/monocle/pair) | package: monocle-lib |
1.3 Lenses for Lists
(require data/monocle/list) | package: monocle-lib |
procedure
idx : exact-nonnegative-integer?
1.4 Lenses for Hashes
(require data/monocle/hash) | package: monocle-lib |
procedure
(&hash-ref* k ...+) → lens?
k : any/c
> ((&hash-ref* 'a 'b 'c) (hasheq 'a (hasheq 'b (hasheq 'c 42)))) 42
> (lens-update (&hash-ref* 'a 'b 'c) (hasheq 'a (hasheq 'b (hasheq 'c 42))) add1) '#hasheq((a . #hasheq((b . #hasheq((c . 43))))))
parameter
(current-hash-maker) → (-> hash?)
(current-hash-maker proc) → void? proc : (-> hash?)
= hasheq
Added in version 0.3 of package monocle-lib.
procedure
(&opt-hash-ref k) → lens?
k : any/c
procedure
(&opt-hash-ref* k ...+) → lens?
k : any/c
Added in version 0.2 of package monocle-lib.
1.5 Lenses for Structs
(require data/monocle/struct) | package: monocle-lib |
syntax
(define-struct-lenses struct-id)
> (struct foo (x y) #:transparent) > (define-struct-lenses foo) > (&foo-x (foo 1 2)) 1
> (&foo-x (foo 1 2) 3) (foo 3 2)