diff --git a/lens/private/compound/compose.rkt b/lens/private/compound/compose.rkt index 71b0efa..934a1fc 100644 --- a/lens/private/compound/compose.rkt +++ b/lens/private/compound/compose.rkt @@ -7,7 +7,6 @@ require racket/contract "../base/main.rkt" "../util/rest-contract.rkt" "identity.rkt" - unstable/lens/isomorphism/base module+ test require rackunit @@ -29,15 +28,7 @@ provide (define (lens-compose . args) - (match args - [(list) - identity-lens] - [(list (make-isomorphism-lens fs invs) ...) - (make-isomorphism-lens - (apply compose1 fs) - (apply compose1 (reverse invs)))] - [_ - (foldr lens-compose2 identity-lens args)])) + (foldr lens-compose2 identity-lens args)) module+ test @@ -52,5 +43,3 @@ module+ test (check-equal? (lens-view first-of-second-lens test-alist) 'b) (check-equal? (lens-set first-of-second-lens test-alist 'B) '((a 1) (B 2) (c 3))) (check-eq? (lens-compose) identity-lens) - (check-pred isomorphism-lens? (lens-compose (make-isomorphism-lens set->list list->set) - (make-isomorphism-lens list->vector vector->list))) diff --git a/unstable/lens/isomorphism.rkt b/unstable/lens/isomorphism.rkt index 7fe763a..6edc5bd 100644 --- a/unstable/lens/isomorphism.rkt +++ b/unstable/lens/isomorphism.rkt @@ -1,3 +1,4 @@ #lang reprovide "isomorphism/base.rkt" +"isomorphism/compound.rkt" "isomorphism/data.rkt" diff --git a/unstable/lens/isomorphism.scrbl b/unstable/lens/isomorphism.scrbl index bcf0f7f..5899e2c 100644 --- a/unstable/lens/isomorphism.scrbl +++ b/unstable/lens/isomorphism.scrbl @@ -46,6 +46,16 @@ example, are defined like this: (make-isomorphism-lenses string->symbol symbol->string)) ]} +@defproc[(isomorphism-compose [lens isomorphism-lens?] ...) isomorphism-lens?]{ +Like @racket[lens-compose], but works only on isomorphism lenses, and returns an +isomorphism lens. It is also more efficient than @racket[lens-compose]. +} + +@defproc[(isomorphism-thrush [lens isomorphism-lens?] ...) isomorphism-lens?]{ +Like @racket[lens-thrush], but works only on isomorphism lenses, and returns an +isomorphism lens. It is also more efficient than @racket[lens-thrush]. +} + @deflenses[[string->symbol-lens symbol->string-lens number->string-lens string->number-lens list->vector-lens vector->list-lens diff --git a/unstable/lens/isomorphism/compound.rkt b/unstable/lens/isomorphism/compound.rkt new file mode 100644 index 0000000..c14bd1b --- /dev/null +++ b/unstable/lens/isomorphism/compound.rkt @@ -0,0 +1,33 @@ +#lang sweet-exp racket/base + +require racket/contract/base +provide + contract-out + isomorphism-compose + (rest-> isomorphism-lens? isomorphism-lens?) + isomorphism-thrush + (rest-> isomorphism-lens? isomorphism-lens?) + +require racket/match + lens/private/util/rest-contract + "base.rkt" +module+ test + require lens/private/base/main + lens/private/compound/identity + rackunit + "data.rkt" + +(define (isomorphism-compose . args) + (match args + [(list (make-isomorphism-lens fs invs) ...) + (make-isomorphism-lens + (apply compose1 fs) + (apply compose1 (reverse invs)))])) + +(define (isomorphism-thrush . args) + (apply isomorphism-compose (reverse args))) + +module+ test + (define string->vector-lens (isomorphism-thrush string->list-lens list->vector-lens)) + (check-equal? (lens-view string->vector-lens "abc") #(#\a #\b #\c)) + (check-equal? (lens-set string->vector-lens "abc" #(#\1 #\2 #\3)) "123")