-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #112 from AlexKnauth/hash-slice
add hash-slice-lens, closes #102
- Loading branch information
Showing
4 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters