From 4bb23d85c2a720c22fcde2268c8ea7d631151bcb Mon Sep 17 00:00:00 2001 From: AlexKnauth Date: Sat, 11 Jul 2015 09:14:40 -0400 Subject: [PATCH] add hash-pluck-lens --- unstable/lens/hash-pluck.rkt | 24 ++++++++++++++++++++++++ unstable/lens/hash-pluck.scrbl | 16 ++++++++++++++++ unstable/lens/main.rkt | 2 ++ unstable/lens/main.scrbl | 1 + 4 files changed, 43 insertions(+) create mode 100644 unstable/lens/hash-pluck.rkt create mode 100644 unstable/lens/hash-pluck.scrbl diff --git a/unstable/lens/hash-pluck.rkt b/unstable/lens/hash-pluck.rkt new file mode 100644 index 0000000..7fdd217 --- /dev/null +++ b/unstable/lens/hash-pluck.rkt @@ -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)) + ) diff --git a/unstable/lens/hash-pluck.scrbl b/unstable/lens/hash-pluck.scrbl new file mode 100644 index 0000000..9d815f7 --- /dev/null +++ b/unstable/lens/hash-pluck.scrbl @@ -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)) +]} diff --git a/unstable/lens/main.rkt b/unstable/lens/main.rkt index 9faf5c1..93a61f5 100644 --- a/unstable/lens/main.rkt +++ b/unstable/lens/main.rkt @@ -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" )) diff --git a/unstable/lens/main.scrbl b/unstable/lens/main.scrbl index 3e84882..2800730 100644 --- a/unstable/lens/main.scrbl +++ b/unstable/lens/main.scrbl @@ -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"]