Skip to content

Commit

Permalink
Merge 2b517bc into 8c969db
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKnauth committed Jul 11, 2015
2 parents 8c969db + 2b517bc commit 3ddb5b1
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
42 changes: 42 additions & 0 deletions unstable/lens/arrow.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#lang racket/base

(provide lens-view->
lens-set->
lens-transform->
lens-view/thrush
lens-set/thrush
lens-transform/thrush
)

(require lens/base/main)
(module+ test
(require rackunit racket/list fancy-app))

(define (lens-view-> target . lenses)
(for/fold ([target target]) ([lens (in-list lenses)])
(lens-view lens target)))

(define (lens-set-> target #:-> new-val . lenses)
(lens-set (apply lens-thrush lenses) target new-val))

(define (lens-transform-> target #:-> transformer . lenses)
(lens-transform (apply lens-thrush lenses) target transformer))

(define lens-view/thrush lens-view->)
(define lens-set/thrush lens-set->)
(define lens-transform/thrush lens-transform->)

(module+ test
(define (set-first l v)
(list* v (rest l)))
(define (set-second l v)
(list* (first l) v (rest (rest l))))
(define first-lens (make-lens first set-first))
(define second-lens (make-lens second set-second))
(check-equal? (lens-view-> '((1 2) 3) first-lens second-lens)
2)
(check-equal? (lens-set-> '((1 2) 3) first-lens second-lens #:-> 'two)
'((1 two) 3))
(check-equal? (lens-transform-> '((1 2) 3) first-lens second-lens #:-> (* 100 _))
'((1 200) 3))
)
52 changes: 52 additions & 0 deletions unstable/lens/arrow.scrbl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#lang scribble/manual

@(require lens/doc-util/main)

@title{lens-view/thrush, lens-set/thrush, and lens-transform/thrush}

@defmodule[unstable/lens/arrow]

@deftogether[[@defproc[(lens-view/thrush [target any/c] [lens lens?] ...) any/c]
@defproc[(lens-view-> [target any/c] [lens lens?] ...) any/c]]]{
Like @racket[lens-view], except that it can take multiple lenses,
which are combined into a nested lens. The argument order is
switched, so that the @racket[target] comes first and the
@racket[lens] arguments come after it.
@racket[(lens-view/thrush target lens ...)] produces the same value as
@racket[(lens-view (lens-thrush lens ...) target)], but can be more
efficient.
The function @racket[lens-view->] is provided as a shorter version.
@lenses-examples[
(lens-view/thrush '(a b ((c d) e f) g) third-lens first-lens second-lens)
(lens-view-> '(a b ((c d) e f) g) third-lens first-lens second-lens)
]}

@deftogether[[@defproc[(lens-set/thrush [target any/c] [lens lens?] ... [#:-> new-view any/c]) any/c]
@defproc[(lens-set-> [target any/c] [lens lens?] ... [#:-> new-view any/c]) any/c]]]{
Like @racket[lens-set], except that it can take multiple lenses,
which again are combined into a nested lens.
@racket[(lens-set/thrush target lens ... #:-> new-view)] is equivalent
to @racket[(lens-set (lens-thrush lens ...) target new-view)], and
@racket[lens-set->] is the shorter version.
@lenses-examples[
(lens-set/thrush '(a b ((c d) e f) g) third-lens first-lens second-lens #:-> "sea")
(lens-set-> '(a b ((c d) e f) g) third-lens first-lens second-lens #:-> "sea")
]}

@deftogether[[@defproc[(lens-transform/thrush [target any/c] [lens lens?] ...
[#:-> transformer (-> any/c any/c)])
any/c]
@defproc[(lens-transform-> [target any/c] [lens lens?] ...
[#:-> transformer (-> any/c any/c)])
any/c]]]{
Like @racket[lens-transform], except that it can take multiple lenses,
just like @racket[lens-set/thrush].
@racket[(lens-transform/thrush target lens ... #:-> transformer)] is
equivalent to
@racket[(lens-transform (lens-thrush lens ...) target transformer)],
and @racket[lens-transform->] is the shorter verison.
@lenses-examples[
(lens-transform/thrush '(a b ((c d) e f) g) third-lens first-lens second-lens #:-> symbol->string)
(lens-transform-> '(a b ((c d) e f) g) third-lens first-lens second-lens #:-> symbol->string)
]}

2 changes: 2 additions & 0 deletions unstable/lens/main.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"hash.rkt"
"view-set.rkt"
"sublist.rkt"
"arrow.rkt"
)

(provide (all-from-out "syntax.rkt"
Expand All @@ -14,4 +15,5 @@
"hash.rkt"
"view-set.rkt"
"sublist.rkt"
"arrow.rkt"
))
1 change: 1 addition & 0 deletions unstable/lens/main.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ this library being backwards-compatible.
@include-section["hash.scrbl"]
@include-section["syntax.scrbl"]
@include-section["sublist.scrbl"]
@include-section["arrow.scrbl"]

0 comments on commit 3ddb5b1

Please sign in to comment.