Skip to content

Commit

Permalink
Merge pull request #112 from AlexKnauth/hash-slice
Browse files Browse the repository at this point in the history
add hash-slice-lens, closes #102
  • Loading branch information
jackfirth committed Jul 15, 2015
2 parents c85bddf + 4bb23d8 commit dafcb70
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
24 changes: 24 additions & 0 deletions unstable/lens/hash-pluck.rkt
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))
)
16 changes: 16 additions & 0 deletions unstable/lens/hash-pluck.scrbl
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))
]}
2 changes: 2 additions & 0 deletions unstable/lens/main.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"sublist.rkt"
"struct.rkt"
"arrow.rkt"
"hash-pluck.rkt"
)

(provide (all-from-out "syntax.rkt"
Expand All @@ -18,4 +19,5 @@
"sublist.rkt"
"struct.rkt"
"arrow.rkt"
"hash-pluck.rkt"
))
1 change: 1 addition & 0 deletions unstable/lens/main.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

0 comments on commit dafcb70

Please sign in to comment.