Add ref nested list and hash lenses
This commit is contained in:
Jack Firth 2015-07-10 00:27:39 -07:00
commit d145cb42c4
10 changed files with 88 additions and 17 deletions

View File

@ -3,7 +3,7 @@
(define collection 'multi)
(define version "1.0")
(define version "1.1")
(define deps

View File

@ -3,7 +3,6 @@
(provide
(contract-out
[list-ref-lens (-> exact-nonnegative-integer? lens?)]
[list-ref-nested-lens (->* () #:rest (listof exact-nonnegative-integer?) lens?)]
[take-lens (-> exact-nonnegative-integer? lens?)]
[drop-lens (-> exact-nonnegative-integer? lens?)]
[first-lens lens?]
@ -61,9 +60,6 @@
(define (list-ref-lens i)
(lens-compose car-lens (drop-lens i)))
(define (list-ref-nested-lens . args)
(apply lens-thrush (map list-ref-lens args)))
(define first-lens (list-ref-lens 0))
(define second-lens (list-ref-lens 1))
(define third-lens (list-ref-lens 2))
@ -86,6 +82,4 @@
(check-equal? (lens-set second-lens '(1 2 3 4 5) 'a) '(1 a 3 4 5))
(check-equal? (lens-set third-lens '(1 2 3 4 5) 'a) '(1 2 a 4 5))
(check-equal? (lens-set fourth-lens '(1 2 3 4 5) 'a) '(1 2 3 a 5))
(check-equal? (lens-set fifth-lens '(1 2 3 4 5) 'a) '(1 2 3 4 a))
(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))))
(check-equal? (lens-set fifth-lens '(1 2 3 4 5) 'a) '(1 2 3 4 a)))

View File

@ -17,6 +17,5 @@
)
focus-lens
drop-lens
list-ref-nested-lens
take-lens
use-applicable-lenses!))

View File

@ -3,6 +3,7 @@
@(require scribble/eval
lens/lenses-examples
(for-label lens
unstable/lens
racket/base
racket/contract))

View File

@ -2,25 +2,26 @@
(provide
(contract-out
[hash-ref-lens (-> any/c lens?)]))
[hash-ref-lens (-> any/c lens?)]
[hash-ref-nested-lens (->* () #:rest list? lens?)]))
(require fancy-app
"base/main.rkt")
lens)
(module+ test
(require rackunit))
(define (hash-ref-lens1 key)
(define (hash-ref-lens key)
(make-lens (hash-ref _ key)
(hash-set _ key _)))
(define (hash-ref-lens . keys)
(apply lens-thrush (map hash-ref-lens1 keys)))
(define (hash-ref-nested-lens . keys)
(apply lens-thrush (map hash-ref-lens keys)))
(module+ test
(define a (hash-ref-lens 'a))
(define a-x (hash-ref-lens 'a 'x))
(define a-x (hash-ref-nested-lens 'a 'x))
(let-lens [val ctxt] a (hash 'a 1 'b 2 'c 3)
(check-equal? val 1)
(check-equal? (ctxt 100) (hash 'a 100 'b 2 'c 3)))

32
unstable/lens/hash.scrbl Normal file
View File

@ -0,0 +1,32 @@
#lang scribble/manual
@(require scribble/eval
lens/lenses-examples
(for-label lens
unstable/lens
racket/base
racket/contract))
@title{Hash Lenses}
@defmodule[unstable/lens/hash]
@defproc[(hash-ref-lens [key any/c]) lens?]{
Constructs a lens that targets hashes and views the value
of @racket[key].
@lenses-unstable-examples[
(define foo-lens (hash-ref-lens 'foo))
(lens-view foo-lens (hash 'foo 10 'bar 20))
(lens-set foo-lens (hash 'foo 10 'bar 20) 1000)
]}
@defproc[(hash-ref-nested-lens [key any/c] ...) lens?]{
Contructs a lens that targets hashes with nested hashes
as values and views the value obtained by using each
@racket[key] in order.
@lenses-unstable-examples[
(define foo-bar-lens (hash-ref-nested-lens 'foo 'bar))
(lens-view foo-bar-lens (hash 'foo (hash 'bar 1)))
(lens-set foo-bar-lens (hash 'foo (hash 'bar 1)) 1000)
]}

17
unstable/lens/list.rkt Normal file
View File

@ -0,0 +1,17 @@
#lang racket
(require lens)
(module+ test
(require rackunit))
(provide
(contract-out
[list-ref-nested-lens (->* () #:rest (listof exact-nonnegative-integer?) lens?)]))
(define (list-ref-nested-lens . args)
(apply lens-thrush (map list-ref-lens args)))
(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))))

21
unstable/lens/list.scrbl Normal file
View File

@ -0,0 +1,21 @@
#lang scribble/manual
@(require scribble/eval
lens/lenses-examples
(for-label lens
racket/base
racket/contract))
@title{List Lenses}
@defmodule[unstable/lens/list]
@defproc[(list-ref-nested-lens [index exact-nonnegative-integer?] ...) lens?]{
Constructs a lens that views into a tree made from nested lists.
Indexing starts from zero in the same was as @racket[list-ref-lens].
@lenses-unstable-examples[
(define first-of-second-lens (list-ref-nested-lens 1 0))
(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)
]}

View File

@ -1,7 +1,11 @@
#lang racket
(require "syntax.rkt"
"compound.rkt")
"compound.rkt"
"list.rkt"
"hash.rkt")
(provide (all-from-out "syntax.rkt"
"compound.rkt"))
"compound.rkt"
"list.rkt"
"hash.rkt"))

View File

@ -10,4 +10,6 @@ may change in future releases. Do not depend on
this library being backwards-compatible.
@include-section["compound.scrbl"]
@include-section["list.scrbl"]
@include-section["hash.scrbl"]
@include-section["syntax.scrbl"]