Merge pull request #208 from AlexKnauth/iso-compose

add isomorphism-compose and isomorphism-thrush
This commit is contained in:
Jack Firth 2015-09-03 15:50:50 -07:00
commit cd3bb022d8
4 changed files with 45 additions and 12 deletions

View File

@ -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)))

View File

@ -1,3 +1,4 @@
#lang reprovide
"isomorphism/base.rkt"
"isomorphism/compound.rkt"
"isomorphism/data.rkt"

View File

@ -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

View File

@ -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")