[top][index]
search for:

applyPairs -- apply a function to pairs in a hash table

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.

It is an error for the function f to return two pairs with the same key.

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 -- apply a function to each value
  • applyKeys -- apply a function to each key in a hash table
  • scanPairs -- apply a function to pairs in a hash table
  • Ways to use applyPairs :

  • applyPairs(HashTable,Function)

  • [top][index]
    search for: