Macaulay2 » Documentation
Packages » Macaulay2Doc » The Macaulay2 language » hash tables » mapping over hash tables » applyKeys
next | previous | forward | backward | up | index | toc

# applyKeys -- apply a function to each key in a hash table

- Usage:
    `applyKeys(H, f)`
    `applyKeys(H, f, g)`
- Inputs:
  - H, a hash table,
  - f, a function, of one argument
  - g, a function, of two arguments, for collision handling
- Outputs:
  - a hash table, obtained by applying f to each key in H

## Description

    i1 : H = new HashTable from {1 => 10, 2 => 15, 3 => 20}

    o1 = HashTable{1 => 10}
                   2 => 15
                   3 => 20

    o1 : HashTable

    i2 : applyKeys(H, k -> k + 100)

    o2 = HashTable{101 => 10}
                   102 => 15
                   103 => 20

    o2 : HashTable

    i3 : applyKeys(H, k -> k//2, max)

    o3 = HashTable{0 => 10}
                   1 => 20

    o3 : HashTable

    i4 : applyKeys(H, k -> k//2, plus)

    o4 = HashTable{0 => 10}
                   1 => 35

    o4 : HashTable

## Caveat

It is an error for the function f to return two pairs with the same key. When this is a possibility, use the function g to specify how the two pairs should be reconciled.

## See also

- hash tables
- applyPairs -- apply a function to each pair in a hash table
- applyValues -- apply a function to each value in a hash table
- keys -- keys used in a hash table, dictionary, or database
- scanKeys -- apply a function to each key in a hash table or database

## Ways to use applyKeys:

- `applyKeys(HashTable,Function)`
- `applyKeys(HashTable,Function,Function)`

## For the programmer

The object applyKeys is a compiled function.

The source of this document is in /build/macaulay2-88fgJW/macaulay2-1.25.11+ds/M2/Macaulay2/packages/Macaulay2Doc/functions/applyKeys-doc.m2:40:0.