Merge pull request #139 from jackfirth/rename-pluck-#136
Rename pluck #136
This commit is contained in:
commit
92d68cf5f0
|
@ -1,15 +1,16 @@
|
|||
#lang racket/base
|
||||
|
||||
(provide hash-pluck-lens)
|
||||
(provide hash-pick-lens)
|
||||
|
||||
(require racket/list
|
||||
lens/base/main
|
||||
"hash.rkt"
|
||||
"join.rkt")
|
||||
|
||||
(module+ test
|
||||
(require rackunit))
|
||||
|
||||
(define (hash-pluck-lens . ks)
|
||||
(define (hash-pick-lens . ks)
|
||||
(apply lens-join/hash
|
||||
(append-map
|
||||
(λ (k)
|
||||
|
@ -17,8 +18,8 @@
|
|||
ks)))
|
||||
|
||||
(module+ test
|
||||
(check-equal? (lens-view (hash-pluck-lens 'a 'c) (hash 'a 1 'b 2 'c 3))
|
||||
(check-equal? (lens-view (hash-pick-lens 'a 'c) (hash 'a 1 'b 2 'c 3))
|
||||
(hash 'a 1 'c 3))
|
||||
(check-equal? (lens-set (hash-pluck-lens 'a 'c) (hash 'a 1 'b 2 'c 3) (hash 'a 4 'c 5))
|
||||
(check-equal? (lens-set (hash-pick-lens 'a 'c) (hash 'a 1 'b 2 'c 3) (hash 'a 4 'c 5))
|
||||
(hash 'a 4 'b 2 'c 5))
|
||||
)
|
|
@ -4,13 +4,13 @@
|
|||
|
||||
@title{Viewing a subset of a hash table by key}
|
||||
|
||||
@defmodule[unstable/lens/hash-pluck]
|
||||
@defmodule[unstable/lens/hash-pick]
|
||||
|
||||
@defproc[(hash-pluck-lens [key any/c] ...) lens?]{
|
||||
@defproc[(hash-pick-lens [key any/c] ...) lens?]{
|
||||
Creates a lens that views a subset of the target hash-table with the given
|
||||
@racket[key]s. The view, is another hash-table with only the given keys and
|
||||
their corrosponding values in the target hash-table.
|
||||
@lenses-unstable-examples[
|
||||
(lens-view (hash-pluck-lens 'a 'c) (hash 'a 1 'b 2 'c 3))
|
||||
(lens-set (hash-pluck-lens 'a 'c) (hash 'a 1 'b 2 'c 3) (hash 'a 4 'c 5))
|
||||
(lens-view (hash-pick-lens 'a 'c) (hash 'a 1 'b 2 'c 3))
|
||||
(lens-set (hash-pick-lens 'a 'c) (hash 'a 1 'b 2 'c 3) (hash 'a 4 'c 5))
|
||||
]}
|
|
@ -10,7 +10,7 @@
|
|||
"sublist.rkt"
|
||||
"struct.rkt"
|
||||
"arrow.rkt"
|
||||
"hash-pluck.rkt"
|
||||
"hash-pick.rkt"
|
||||
"stream.rkt"
|
||||
)
|
||||
|
||||
|
@ -24,6 +24,6 @@
|
|||
"sublist.rkt"
|
||||
"struct.rkt"
|
||||
"arrow.rkt"
|
||||
"hash-pluck.rkt"
|
||||
"hash-pick.rkt"
|
||||
"stream.rkt"
|
||||
))
|
||||
|
|
|
@ -19,5 +19,5 @@ this library being backwards-compatible.
|
|||
@include-section["sublist.scrbl"]
|
||||
@include-section["struct.scrbl"]
|
||||
@include-section["arrow.scrbl"]
|
||||
@include-section["hash-pluck.scrbl"]
|
||||
@include-section["hash-pick.scrbl"]
|
||||
@include-section["stream.scrbl"]
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
#lang racket/base
|
||||
|
||||
(provide string-ref-lens
|
||||
string-pluck-lens
|
||||
)
|
||||
string-pick-lens)
|
||||
|
||||
(require fancy-app
|
||||
lens/base/main
|
||||
"join.rkt"
|
||||
)
|
||||
"join.rkt")
|
||||
|
||||
(module+ test
|
||||
(require rackunit))
|
||||
|
||||
|
||||
(define (string-ref-lens i)
|
||||
(make-lens
|
||||
(string-ref _ i)
|
||||
|
@ -24,20 +24,17 @@
|
|||
c
|
||||
(string-ref s j))))))
|
||||
|
||||
(define (string-pluck-lens . is)
|
||||
(module+ test
|
||||
(check-equal? (lens-view (string-ref-lens 2) "abc") #\c)
|
||||
(check-equal? (lens-set (string-ref-lens 0) "abc" #\A) "Abc"))
|
||||
|
||||
|
||||
(define (string-pick-lens . is)
|
||||
(apply lens-join/string (map string-ref-lens is)))
|
||||
|
||||
|
||||
(module+ test
|
||||
(check-equal? (lens-view (string-ref-lens 0) "abc") #\a)
|
||||
(check-equal? (lens-view (string-ref-lens 1) "abc") #\b)
|
||||
(check-equal? (lens-view (string-ref-lens 2) "abc") #\c)
|
||||
(check-equal? (lens-set (string-ref-lens 0) "abc" #\A) "Abc")
|
||||
(check-equal? (lens-set (string-ref-lens 1) "abc" #\B) "aBc")
|
||||
(check-equal? (lens-set (string-ref-lens 2) "abc" #\C) "abC")
|
||||
(define 1-5-6-lens (string-pluck-lens 1 5 6))
|
||||
(define 1-5-6-lens (string-pick-lens 1 5 6))
|
||||
(check-equal? (lens-view 1-5-6-lens "abcdefg")
|
||||
"bfg")
|
||||
(check-equal? (lens-set 1-5-6-lens "abcdefg" "BFG")
|
||||
"aBcdeFG")
|
||||
)
|
||||
"aBcdeFG"))
|
||||
|
|
|
@ -11,11 +11,11 @@ Returns a lens for viewing the @racket[i]th character of a string.
|
|||
(lens-set (string-ref-lens 2) "abcdef" #\C)
|
||||
]}
|
||||
|
||||
@defproc[(string-pluck-lens [i exact-nonnegative-integer?]) lens?]{
|
||||
@defproc[(string-pick-lens [i exact-nonnegative-integer?]) lens?]{
|
||||
Like @racket[list-refs-lens], but for strings.
|
||||
Equivalent to @racket[(lens-join/string (string-ref-lens i) ...)].
|
||||
@lenses-unstable-examples[
|
||||
(define 1-5-6-lens (string-pluck-lens 1 5 6))
|
||||
(define 1-5-6-lens (string-pick-lens 1 5 6))
|
||||
(lens-view 1-5-6-lens "abcdefg")
|
||||
(lens-set 1-5-6-lens "abcdefg" "BFG")
|
||||
]}
|
||||
|
|
|
@ -2,17 +2,17 @@
|
|||
|
||||
(provide vector-ref-lens
|
||||
vector-ref-nested-lens
|
||||
vector-pluck-lens
|
||||
)
|
||||
vector-pick-lens)
|
||||
|
||||
(require fancy-app
|
||||
lens/base/main
|
||||
"arrow.rkt"
|
||||
"join.rkt"
|
||||
)
|
||||
"join.rkt")
|
||||
|
||||
(module+ test
|
||||
(require rackunit))
|
||||
|
||||
|
||||
(define (vector-ref-lens i)
|
||||
(make-lens
|
||||
(vector-ref _ i)
|
||||
|
@ -26,25 +26,27 @@
|
|||
x
|
||||
(vector-ref v j))))))
|
||||
|
||||
(module+ test
|
||||
(check-equal? (lens-view (vector-ref-lens 0) #(a b c)) 'a)
|
||||
(check-equal? (lens-set (vector-ref-lens 2) #(a b c) "C") #(a b "C")))
|
||||
|
||||
|
||||
(define (vector-ref-nested-lens . is)
|
||||
(apply lens-thrush (map vector-ref-lens is)))
|
||||
|
||||
(define (vector-pluck-lens . is)
|
||||
(module+ test
|
||||
(check-equal? (lens-transform (vector-ref-nested-lens 2 1)
|
||||
#(a #(b c) #(d e f))
|
||||
symbol->string)
|
||||
#(a #(b c) #(d "e" f))))
|
||||
|
||||
|
||||
(define (vector-pick-lens . is)
|
||||
(apply lens-join/vector (map vector-ref-lens is)))
|
||||
|
||||
|
||||
(module+ test
|
||||
(check-equal? (lens-view (vector-ref-lens 0) #(a b c)) 'a)
|
||||
(check-equal? (lens-view (vector-ref-lens 1) #(a b c)) 'b)
|
||||
(check-equal? (lens-view (vector-ref-lens 2) #(a b c)) 'c)
|
||||
(check-equal? (lens-set (vector-ref-lens 0) #(a b c) "A") #("A" b c))
|
||||
(check-equal? (lens-set (vector-ref-lens 1) #(a b c) "B") #(a "B" c))
|
||||
(check-equal? (lens-set (vector-ref-lens 2) #(a b c) "C") #(a b "C"))
|
||||
(check-equal? (lens-transform (vector-ref-nested-lens 2 1) #(a #(b c) #(d e f)) symbol->string)
|
||||
#(a #(b c) #(d "e" f)))
|
||||
(define 1-5-6-lens (vector-pluck-lens 1 5 6))
|
||||
(define 1-5-6-lens (vector-pick-lens 1 5 6))
|
||||
(check-equal? (lens-view 1-5-6-lens #(a b c d e f g))
|
||||
#(b f g))
|
||||
(check-equal? (lens-set 1-5-6-lens #(a b c d e f g) #(1 2 3))
|
||||
#(a 1 c d e 2 3))
|
||||
)
|
||||
#(a 1 c d e 2 3)))
|
||||
|
|
|
@ -19,11 +19,11 @@ Equivalent to @racket[(lens-thrush (vector-ref-lens i) ...)].
|
|||
(lens-set (vector-ref-nested-lens 2 1) #(a b #(s i) d) "eye")
|
||||
]}
|
||||
|
||||
@defproc[(vector-pluck-lens [i exact-nonnegative-integer?] ...) lens?]{
|
||||
@defproc[(vector-pick-lens [i exact-nonnegative-integer?] ...) lens?]{
|
||||
Like @racket[list-refs-lens], but for vectors.
|
||||
Equivalent to @racket[(lens-join/vector (vector-ref-lens i) ...)].
|
||||
@lenses-unstable-examples[
|
||||
(define 1-5-6-lens (vector-pluck-lens 1 5 6))
|
||||
(define 1-5-6-lens (vector-pick-lens 1 5 6))
|
||||
(lens-view 1-5-6-lens #(a b c d e f g))
|
||||
(lens-set 1-5-6-lens #(a b c d e f g) #(1 2 3))
|
||||
]}
|
||||
|
|
Loading…
Reference in New Issue
Block a user