▼ |  | Unstable: May Change Without Warning

1 | Guidelines for Developing unstable Libraries
2 | Automata: Compiling State Machines
3 | Bytes
4 | Contracts
5 | Converting Values to Strings
6 | Contracts for Macro Subexpressions
7 | Debugging
8 | Definitions
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 | Mark Parameters
20 | Match
21 | Parameter Groups
22 | Pretty-Printing
23 | Sequences
24 | Strings
25 | Structs
26 | Syntax
27 | Temporal Contracts: Explicit Contract Monitors
28 | GUI Libraries

On this page:

hash-union
hash-union!

Version: 5.3
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 →