On this page:
hash-union
hash-union!
6.2.1

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