▼ |  | Unstable: May Change Without Warning

1 | Guidelines for Developing unstable Libraries
2 | Automata: Compiling State Machines
3 | Bytes
4 | Contracts
5 | Contracts for Macro Subexpressions
6 | Debugging
7 | Definitions
8 | Errors
9 | Filesystem
10 | Find
11 | Flonums
12 | Futures
13 | Functions
14 | Hash Tables
15 | Interface-Oriented Programming for Classes
16 | Lazy Require
17 | Lists
18 | Logging
19 | Macro Testing
20 | Mark Parameters
21 | Match
22 | Open place expressions
23 | Options
24 | Parameter Groups
25 | Pretty-Printing
26 | Re-Contracting Identifiers
27 | Sandbox
28 | Sequences
29 | Strings
30 | Structs
31 | Struct Printing
32 | Syntax
33 | Temporal Contracts: Explicit Contract Monitors
34 | Unix Domain Sockets
35 | 2D Syntax
36 | GUI Libraries

On this page:

hash-union
hash-union!

5.3.6
top ← prev up next →

### 14`` Hash Tables

Carl Eastlund <cce@racket-lang.org>

This library is unstable; compatibility will not be maintained. See Unstable: May Change Without Warning for more information.

(require unstable/hash)

This module provides tools for manipulating hash tables.

> > procedure

> > (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)))

> > procedure

> > (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((2 . (two dos)) (1 . (one uno)))

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

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

top ← prev up next →