Add list-refs lens
This commit is contained in:
Jack Firth 2015-07-10 21:28:28 -07:00
commit edaffda938
2 changed files with 27 additions and 4 deletions

View File

@ -1,17 +1,31 @@
#lang racket
(require lens)
(require lens
"compound.rkt")
(module+ test
(require rackunit))
(provide
(contract-out
[list-ref-nested-lens (->* () #:rest (listof exact-nonnegative-integer?) lens?)]))
[list-ref-nested-lens (->* () #:rest (listof exact-nonnegative-integer?) lens?)]
[list-refs-lens (->* () #:rest (listof exact-nonnegative-integer?) lens?)]))
(define (list-ref-nested-lens . args)
(apply lens-thrush (map list-ref-lens args)))
(define (list-ref-nested-lens . indices)
(apply lens-thrush (map list-ref-lens indices)))
(module+ test
(check-equal? (lens-transform/list '(a (b c) (d e f)) (list-ref-nested-lens 2 1) symbol->string)
'(a (b c) (d "e" f))))
(define (list-refs-lens . indices)
(apply compound-list-lens (map list-ref-lens indices)))
(module+ test
(define 1-5-6-lens (list-refs-lens 1 5 6))
(check-equal? (lens-view 1-5-6-lens '(a b c d e f g))
'(b f g))
(check-equal? (lens-set 1-5-6-lens '(a b c d e f g) '(1 2 3))
'(a 1 c d e 2 3)))

View File

@ -15,3 +15,12 @@
(lens-view first-of-second-lens '(1 (a b c) 2 3))
(lens-set first-of-second-lens '(1 (a b c) 2 3) 'foo)
]}
@defproc[(list-refs-lens [index exact-nonnegative-integer?] ...) lens?]{
Constructs a lens that views each @racket[index] item in a list.
Indexing starts from zero in the same was as @racket[list-ref-lens].
@lenses-unstable-examples[
(define 1-5-6-lens (list-refs-lens 1 5 6))
(lens-view 1-5-6-lens '(a b c d e f g))
(lens-set 1-5-6-lens '(a b c d e f g) '(1 2 3))
]}