36 Hash Tables

▼ |  | Unstable

1 | Guidelines for developing unstable libraries
2 | Bytes
3 | Classes and Objects
4 | Contracts
5 | Definitions
6 | Dictionaries
7 | Directories
8 | Exceptions
9 | Filesystem
10 | Functions
11 | Lists
12 | Net
13 | Path
14 | PLane T Packages
15 | Ports
16 | Pretty-Printing
17 | Regular Expressions
18 | Requiring Modules
19 | Sandboxed Evaluation
20 | Scribble Documentation
21 | Sets
22 | S-Expression Diff
23 | Source Locations
24 | Strings
25 | Structs
26 | Syntax
27 | Planet Package Macros
28 | Text Representations
29 | Multiple Values
30 | XML and CSS
31 | Polymorphic Contracts
32 | Finding Mutated Variables
33 | Find
34 | Interface-Oriented Programming for Classes
35 | Sequences
36 | Hash Tables
37 | Match
38 | Generics
39 | Mark Parameters
40 | Debugging
41 | Contracts for macro subexpressions
42 | GUI libraries

► | 36 | Hash Tables

36.1 | Hash Table Lookup
36.2 | Hash Table Combinations

On this page:

36.1`` Hash Table Lookup
hash-ref/ check
hash-ref/ identity
hash-ref/ default
hash-ref/ failure
36.2`` Hash Table Combinations
hash-union
hash-union!

Version: 5.0.1.5
top← prev up next →

### 36`` Hash Tables

Carl Eastlund <cce@racket-lang.org>

( require unstable/hash)

This library is unstable; compatibility will not be maintained. See Unstable for more information.

This module provides tools for manipulating hash tables.

#### 36.1`` Hash Table Lookup

( hash-ref/check h k) → any/c
h : hash?
k : ( lambda (k) ( hash-has-key? h k))

Looks up key k in hash table h. Raises a contract error if h has no entry for k. Equivalent to ( hash-ref h k), except for the specific exception value raised.

Example:
> ( hash-ref/check ( make-immutable-hash '([1 . one] [2 . two] [3 . three])) 2)
'two

( hash-ref/identity h k) → any/c
h : hash?
k : any/c

Looks up key k in hash table h. Returns k if h has no entry for k. Equivalent to ( hash-ref h k ( lambda () k)).

Examples:
> ( hash-ref/identity ( make-immutable-hash '([1 . one] [2 . two] [3 . three])) 2)
'two
> ( hash-ref/identity ( make-immutable-hash '([1 . one] [2 . two] [3 . three])) 4)
4

( hash-ref/default h k v) → any/c
h : hash?
k : any/c
v : any/c

Looks up key k in hash table h. Returns v if h has no entry for k. Equivalent to ( hash-ref h k ( lambda () v)).

Examples:
> ( hash-ref/default ( make-immutable-hash '([1 . one] [2 . two] [3 . three])) 2 'other)
'two
> ( hash-ref/default ( make-immutable-hash '([1 . one] [2 . two] [3 . three])) 4 'other)
'other

( hash-ref/failure h k f) → any/c
h : hash?
k : any/c
f : (-> any/c)

Looks up key k in hash table h. Returns the result of applying f (in tail position) if h has no entry for k. Equivalent to ( hash-ref h k f).

Examples:
> ( hash-ref/failure ( make-immutable-hash '([1 . one] [2 . two] [3 . three])) 2 gensym)
'two
> ( hash-ref/failure ( make-immutable-hash '([1 . one] [2 . two] [3 . three])) 4 gensym)
'g2151

#### 36.2`` Hash Table Combinations

( hash-union |  | h0 |
|  | h ... |
| [ | #:combine combine |
|  | #:combine/key combine/key]) |

→ ( and/c hash? hash-can-functional-set?)
h0 : ( and/c hash? hash-can-functional-set?)
h : hash?

| combine |  | : |  | (-> any/c any/c any/c)
|  |  | = |  | ( lambda _ ( error 'hash-union ...))

| combine/key |  | : |  | (-> any/c any/c any/c any/c)
|  |  | = |  | ( lambda (k a b) (combine a b))

Computes the union of h0 with each hash table h by functional update, adding each element of each h to h0 in turn. For each key k and value v, if a mapping from k to some value v0 already exists, it is replaced with a mapping from k to (combine/key k v0 v).

Examples:
> ( hash-union ( make-immutable-hash '([1 . one])) ( make-immutable-hash '([2 . two])) ( make-immutable-hash '([3 . three])))
'#hash((1 . one) (2 . two) (3 . three))

> ( hash-union ( make-immutable-hash '([1 one uno] [2 two dos]))
( make-immutable-hash '([1 ein une] [2 zwei deux]))
#:combine/key ( lambda (k v1 v2) ( append v1 v2)))

'#hash((1 . (one uno ein une)) (2 . (two dos zwei deux)))

( hash-union! |  | h0 |  |  |  |
|  | h ... |  |  |  |
| [ | #:combine combine |  |  |  |
|  | #:combine/key combine/key]) |  | → |  | void?

h0 : ( and/c hash? hash-mutable?)
h : hash?

| combine |  | : |  | (-> any/c any/c any/c)
|  |  | = |  | ( lambda _ ( error 'hash-union ...))

| combine/key |  | : |  | (-> any/c any/c any/c any/c)
|  |  | = |  | ( lambda (k a b) (combine a b))

Computes the union of h0 with each hash table h by mutable update, adding each element of each h to h0 in turn. For each key k and value v, if a mapping from k to some value v0 already exists, it is replaced with a mapping from k to (combine/key k v0 v).

Examples:

( define h ( make-hash))

> h
'#hash()
> ( hash-union! h ( make-immutable-hash '([1 one uno] [2 two dos])))
> h
'#hash((1 . (one uno)) (2 . (two dos)))

> ( hash-union! h
( make-immutable-hash '([1 ein une] [2 zwei deux]))
#:combine/key ( lambda (k v1 v2) ( append v1 v2)))

> h
'#hash((1 . (one uno ein une)) (2 . (two dos zwei deux)))

top← prev up next →