Merge pull request #94 from jackfirth/feature-hash-list-lenses-#81
Add ref nested list and hash lenses
This commit is contained in:
commit
d145cb42c4
2
info.rkt
2
info.rkt
|
@ -3,7 +3,7 @@
|
|||
(define collection 'multi)
|
||||
|
||||
|
||||
(define version "1.0")
|
||||
(define version "1.1")
|
||||
|
||||
|
||||
(define deps
|
||||
|
|
|
@ -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)))
|
||||
|
|
|
@ -17,6 +17,5 @@
|
|||
)
|
||||
focus-lens
|
||||
drop-lens
|
||||
list-ref-nested-lens
|
||||
take-lens
|
||||
use-applicable-lenses!))
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
@(require scribble/eval
|
||||
lens/lenses-examples
|
||||
(for-label lens
|
||||
unstable/lens
|
||||
racket/base
|
||||
racket/contract))
|
||||
|
||||
|
|
|
@ -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
32
unstable/lens/hash.scrbl
Normal 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
17
unstable/lens/list.rkt
Normal 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
21
unstable/lens/list.scrbl
Normal 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)
|
||||
]}
|
|
@ -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"))
|
||||
|
|
|
@ -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"]
|
||||
|
|
Loading…
Reference in New Issue
Block a user