Merge pull request #112 from AlexKnauth/hash-slice
add hash-slice-lens, closes #102
This commit is contained in:
commit
dafcb70890
24
unstable/lens/hash-pluck.rkt
Normal file
24
unstable/lens/hash-pluck.rkt
Normal file
|
@ -0,0 +1,24 @@
|
|||
#lang racket/base
|
||||
|
||||
(provide hash-pluck-lens)
|
||||
|
||||
(require racket/list
|
||||
lens/base/main
|
||||
"hash.rkt"
|
||||
"join.rkt")
|
||||
(module+ test
|
||||
(require rackunit))
|
||||
|
||||
(define (hash-pluck-lens . ks)
|
||||
(apply lens-join/hash
|
||||
(append-map
|
||||
(λ (k)
|
||||
(list k (hash-ref-lens k)))
|
||||
ks)))
|
||||
|
||||
(module+ test
|
||||
(check-equal? (lens-view (hash-pluck-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))
|
||||
(hash 'a 4 'b 2 'c 5))
|
||||
)
|
16
unstable/lens/hash-pluck.scrbl
Normal file
16
unstable/lens/hash-pluck.scrbl
Normal file
|
@ -0,0 +1,16 @@
|
|||
#lang scribble/manual
|
||||
|
||||
@(require lens/doc-util/main)
|
||||
|
||||
@title{Viewing a subset of a hash table by key}
|
||||
|
||||
@defmodule[unstable/lens/hash-pluck]
|
||||
|
||||
@defproc[(hash-pluck-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-slice-lens 'a 'c) (hash 'a 1 'b 2 'c 3))
|
||||
(lens-set (hash-slice-lens 'a 'c) (hash 'a 1 'b 2 'c 3) (hash 'a 4 'c 5))
|
||||
]}
|
|
@ -8,6 +8,7 @@
|
|||
"sublist.rkt"
|
||||
"struct.rkt"
|
||||
"arrow.rkt"
|
||||
"hash-pluck.rkt"
|
||||
)
|
||||
|
||||
(provide (all-from-out "syntax.rkt"
|
||||
|
@ -18,4 +19,5 @@
|
|||
"sublist.rkt"
|
||||
"struct.rkt"
|
||||
"arrow.rkt"
|
||||
"hash-pluck.rkt"
|
||||
))
|
||||
|
|
|
@ -17,3 +17,4 @@ this library being backwards-compatible.
|
|||
@include-section["sublist.scrbl"]
|
||||
@include-section["struct.scrbl"]
|
||||
@include-section["arrow.scrbl"]
|
||||
@include-section["hash-pluck.scrbl"]
|
||||
|
|
Loading…
Reference in New Issue
Block a user