applyPairs

applyPairs(x,f) -- applies f to each pair (k,v) in the hash table x to produce a new hash table.

It produces a new hash table y from a hash table x by applying the function f to the pair (k,v) for each key k, where v is the value stored in x as x#k. Thus f should be a function of two variables which returns either a pair (kk,vv) which is placed into y, or it returns null, which signifies that no action be performed.

In this example, we show how to produce the hash table corresponding to the inverse of a function.

i1 : x = new HashTable from {1 => a, 2 => b, 3 => c}

o1 = HashTable{1 => a}
               2 => b
               3 => c

o1 : HashTable
i2 : y = applyPairs(x, (k,v) -> (v,k))

o2 = HashTable{a => 1}
               b => 2
               c => 3

o2 : HashTable
i3 : x#2

o3 = b

o3 : Symbol
i4 : y#b

o4 = 2

See also applyValues, applyKeys, and scanPairs.


topindexpreviousupnext