From 1ab56f079407f3411046c8e6984e0e7747418391 Mon Sep 17 00:00:00 2001 From: Jack Firth Date: Fri, 10 Jul 2015 21:23:19 -0700 Subject: [PATCH] Add list-refs lens --- unstable/lens/list.rkt | 22 ++++++++++++++++++---- unstable/lens/list.scrbl | 9 +++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/unstable/lens/list.rkt b/unstable/lens/list.rkt index 36b2747..bce7392 100644 --- a/unstable/lens/list.rkt +++ b/unstable/lens/list.rkt @@ -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))) diff --git a/unstable/lens/list.scrbl b/unstable/lens/list.scrbl index e8852ee..0a6be1a 100644 --- a/unstable/lens/list.scrbl +++ b/unstable/lens/list.scrbl @@ -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)) +]}