From ec08ef67dd8ffcb19d8aa7225454a9501dbc911d Mon Sep 17 00:00:00 2001 From: AlexKnauth Date: Thu, 3 Sep 2015 02:11:10 -0400 Subject: [PATCH 1/3] add isomorphism-compose and isomorphism-thrush also take away special case in lens-compose for isomorphisms --- lens/private/compound/compose.rkt | 13 +------------ unstable/lens/isomorphism.rkt | 1 + unstable/lens/isomorphism.scrbl | 10 ++++++++++ unstable/lens/isomorphism/compound.rkt | 18 ++++++++++++++++++ 4 files changed, 30 insertions(+), 12 deletions(-) create mode 100644 unstable/lens/isomorphism/compound.rkt 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..244da9c 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. +} + +@defproc[(isomorphism-thrush [lens isomorphism-lens?] ...) isomorphism-lens?]{ +Like @racket[lens-thrush], but works only on isomorphism lenses, and returns an +isomorphism lens. +} + @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..26b7d4f --- /dev/null +++ b/unstable/lens/isomorphism/compound.rkt @@ -0,0 +1,18 @@ +#lang sweet-exp racket/base + +provide isomorphism-compose + isomorphism-thrush + +require racket/match + "base.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))) + From 7e512ada886eb157a220c4225dcfe7a7bdf51a5b Mon Sep 17 00:00:00 2001 From: AlexKnauth Date: Thu, 3 Sep 2015 16:24:05 -0400 Subject: [PATCH 2/3] add contracts and tests for isomorphism-compose and -thrush --- unstable/lens/isomorphism/compound.rkt | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/unstable/lens/isomorphism/compound.rkt b/unstable/lens/isomorphism/compound.rkt index 26b7d4f..c14bd1b 100644 --- a/unstable/lens/isomorphism/compound.rkt +++ b/unstable/lens/isomorphism/compound.rkt @@ -1,10 +1,21 @@ #lang sweet-exp racket/base -provide isomorphism-compose - isomorphism-thrush +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 @@ -16,3 +27,7 @@ require racket/match (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") From cb2f192ed926b15563193d85defb57520744d2f1 Mon Sep 17 00:00:00 2001 From: AlexKnauth Date: Thu, 3 Sep 2015 16:27:48 -0400 Subject: [PATCH 3/3] add note about isomorphism-compose being more efficient --- unstable/lens/isomorphism.scrbl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/unstable/lens/isomorphism.scrbl b/unstable/lens/isomorphism.scrbl index 244da9c..5899e2c 100644 --- a/unstable/lens/isomorphism.scrbl +++ b/unstable/lens/isomorphism.scrbl @@ -48,12 +48,12 @@ example, are defined like this: @defproc[(isomorphism-compose [lens isomorphism-lens?] ...) isomorphism-lens?]{ Like @racket[lens-compose], but works only on isomorphism lenses, and returns an -isomorphism lens. +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. +isomorphism lens. It is also more efficient than @racket[lens-thrush]. } @deflenses[[string->symbol-lens symbol->string-lens