▼ |  | 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 | Errors
10 | Filesystem
11 | Find
12 | Flonums
13 | Futures
14 | Functions
15 | Hash Tables
16 | Interface-Oriented Programming for Classes
17 | Lazy Require
18 | Lists
19 | Logging
20 | Mark Parameters
21 | Match
22 | Open place expressions
23 | Parameter Groups
24 | Pretty-Printing
25 | Re-Contracting Identifiers
26 | Sandbox
27 | Sequences
28 | Strings
29 | Structs
30 | Struct Printing
31 | Syntax
32 | Temporal Contracts: Explicit Contract Monitors
33 | GUI Libraries

On this page:

hash-union
hash-union!

Version: 5.3.1
top ← prev up next →

### 15`` 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 →