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

On this page:

hash-union
hash-union!

5.92
top ← prev up next →

### 11`` 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) | package: unstable-list-lib

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 →