# merge -- merge two hash tables

## Synopsis

• Usage:
z = merge(x, y, f)
• Inputs:
• x,
• y,
• f, , of two variables specifying how to handle duplicate keys
• Outputs:
• z, , a new hash table whose keys are the union of the keys of x and the keys of y

## Description

Key-value pairs whose keys appear in only one of the input tables appear unchanged in the output table. If the key k appears in both x and y, then f(x#k, y#k) is the value associated to k in the output hash table.

For example, we could take the max of the two values, their average, or make a list containing both.

 ```i1 : x = new HashTable from {1 => 203, 2 => 21, 3 => 5, 4 => 130} o1 = HashTable{1 => 203} 2 => 21 3 => 5 4 => 130 o1 : HashTable``` ```i2 : y = new HashTable from {2 => 37, 3 => 5, 4 => 56, 5 => 1} o2 = HashTable{2 => 37} 3 => 5 4 => 56 5 => 1 o2 : HashTable``` ```i3 : merge(x, y, max) o3 = HashTable{1 => 203} 2 => 37 3 => 5 4 => 130 5 => 1 o3 : HashTable``` ```i4 : merge(x, y, (i,j) -> (i+j)/2) o4 = HashTable{1 => 203} 2 => 29 3 => 5 4 => 93 5 => 1 o4 : HashTable``` ```i5 : merge(x, y, (i,j) -> {i,j}) o5 = HashTable{1 => 203 } 2 => {21, 37} 3 => {5, 5} 4 => {130, 56} 5 => 1 o5 : HashTable```

If the function f(x#k, y#k) returns continue, then the key k is omitted from the merged table.

 ```i6 : merge(x, y, (i,j) -> if i==j then i else continue) o6 = HashTable{1 => 203} 3 => 5 5 => 1 o6 : HashTable```

Here is a simple implementation of the free abelian group on four letters, where each element is represented as a hash table that associates coefficients to strings.

 ```i7 : Free = new Type of HashTable o7 = Free o7 : Type``` ```i8 : p = new Free from { "x" => 2, "y" => 3, "z" => 5 } o8 = Free{x => 2} y => 3 z => 5 o8 : Free``` ```i9 : q = new Free from { "x" => 100, "y" => 200, "w" => 7 } o9 = Free{w => 7 } x => 100 y => 200 o9 : Free``` `i10 : Free + Free := (p,q) -> merge(p, q, plus);` ```i11 : p+q o11 = Free{w => 7 } x => 102 y => 203 z => 5 o11 : Free```

If x and y have the same class and have the same parent, as in the previous example, then so will z.

 `i12 : x = new MutableHashTable from {"alice" => 53709, "bob" => 6549};` `i13 : y = new MutableHashTable from {"bob" => 86, "charlie" => 23};` ```i14 : mutable merge(x, y, plus) o14 = true```

The function combine allows much greater control when combining two hash tables: you can give functions for how to handle every key and value of the input tables, not just the duplicates. The function mergePairs is similar to merge, but works on lists of pairs rather than hash tables.

• applyKeys -- apply a function to each key in a hash table
• applyPairs -- apply a function to each pair in a hash table
• applyValues -- apply a function to each value in a hash table
• combine -- combine hash tables
• keys -- keys used in a hash table
• mergePairs -- merge sorted lists of pairs
• pairs -- list the pairs in a hash table, dictionary, or basic list
• scanKeys -- apply a function to each key in a hash table or database
• scanPairs -- apply a function to the pairs in a hash table
• scanValues -- apply a function to each value in a hash table or database
• values -- values in a hash table
• hash tables

## Ways to use merge :

• merge(HashTable,HashTable,Function)