Macaulay2 / M2

The primary source code repository for Macaulay2, a system for computing in commutative algebra, algebraic geometry and related fields.
https://macaulay2.com
330 stars 226 forks source link

Add select functions for hash tables #3305

Closed d-torrance closed 6 days ago

d-torrance commented 2 weeks ago

We add three new methods (selectKeys, selectValues, and selectPairs) for hash tables, partially addressing #579. That issue also suggested dropping hash table support from select. But that would be a breaking change (there's some code in Macaulay2Doc that uses it, for example), so I'm proposing that we instead keep it around as an alias for selectValues.

We also add select support for sets, using keys instead of values, addressing #578.

For example:

i1 : x = hashTable apply(5, i -> (i, i + 1))

o1 = HashTable{0 => 1}
               1 => 2
               2 => 3
               3 => 4
               4 => 5

o1 : HashTable

i2 : selectKeys(x, odd)

o2 = HashTable{1 => 2}
               3 => 4

o2 : HashTable

i3 : selectValues(x, odd)

o3 = HashTable{0 => 1}
               2 => 3
               4 => 5

o3 : HashTable

i4 : selectPairs(x, (k,v) -> odd(k + 2*v))

o4 = HashTable{1 => 2}
               3 => 4

o4 : HashTable

i5 : select(set(1..10), odd)

o5 = set {9, 1, 3, 5, 7}

o5 : Set