Skip to content

Commit

Permalink
Add selectPairs for non-hash table
Browse files Browse the repository at this point in the history
selectPairs(x, f) is the same as select(pairs x, f)

Doesn't support iterators (yet) since select is defined for them at top-level.
  • Loading branch information
d-torrance committed Dec 18, 2024
1 parent 88ab651 commit 718cf4a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
11 changes: 7 additions & 4 deletions M2/Macaulay2/d/actors4.d
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ select(e:Expr):Expr := (
else WrongNumArgs(2,5));
setupfun("select", select).Protected = false; -- will be overloaded in m2/lists.m2 and m2/regex.m2

-- # typical value: selectPairs, HashTable, Function, HashTable
-- # typical value: selectPairs, ZZ, HashTable, Function, HashTable
selectPairs(nval:int, obj:HashTable, f:Expr):Expr := (
u := newHashTable(obj.Class,obj.parent);
u.beingInitialized = true;
Expand Down Expand Up @@ -228,18 +230,18 @@ selectPairs(nval:int, obj:HashTable, f:Expr):Expr := (
"expected predicate to yield true or false"));
p = p.next));
Expr(sethash(u,obj.Mutable)));
-- TODO: support iterators
selectPairs(e:Expr):Expr := (
when e
is a:Sequence do (
-- # typical value: selectPairs, HashTable, Function, HashTable
if length(a) == 2 then (
when a.0
is obj:HashTable do (
if obj.Mutable
then WrongArgImmutableHashTable(1)
else selectPairs(obj.numEntries, obj, a.1))
else WrongArgHashTable(1))
-- # typical value: selectPairs, ZZ, HashTable, Function, HashTable
-- # typical value: selectPairs, BasicList, Function, Sequence
else select(pairs(a.0), a.1))
else if length(a) == 3 then (
when a.0
is n:ZZcell do (
Expand All @@ -250,7 +252,8 @@ selectPairs(e:Expr):Expr := (
if obj.Mutable
then WrongArgImmutableHashTable(2)
else selectPairs(toInt(n), obj, a.2))
else WrongArgHashTable(2)))
-- # typical value: selectPairs, ZZ, BasicList, Function, Sequence
else select(a.0, pairs(a.1), a.2, nullE, nullE)))
else WrongArgZZ(1))
else WrongNumArgs(2, 3))
else WrongNumArgs(2, 3));
Expand Down
14 changes: 12 additions & 2 deletions M2/Macaulay2/packages/Macaulay2Doc/functions/select-doc.m2
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ doc ///
doc ///
Key
selectPairs
(selectPairs, BasicList, Function)
(selectPairs, HashTable, Function)
(selectPairs, ZZ, BasicList, Function)
(selectPairs, ZZ, HashTable, Function)
Headline
select a part of a hash table by pairs
Expand All @@ -274,10 +276,10 @@ doc ///
selectPairs(n, x, f)
Inputs
n:ZZ
x:HashTable -- must be immutable
x:{HashTable, BasicList}
f:Function
Outputs
:HashTable
:{HashTable, Sequence}
containing all (or @VAR "n"@, if it is given) key-value pairs
(@VAR "k"@,@VAR "v"@) from @VAR "x"@ for which @CODE "f(k,v)"@ evaluates
to true.
Expand All @@ -286,6 +288,14 @@ doc ///
x = hashTable{(1, 2), (2, 4), (3, 6), (4, 8), (5, 10)}
selectPairs(x, (k,v) -> odd(k + v))
selectPairs(2, x, (k, v) -> odd(k + v))
Text
If @CODE "x"@ is not a hash table, then @M2CODE "select(pairs x, f)"@
(or @M2CODE "select(n, pairs x, f)"@) is called.
Example
selectPairs(toList(1..10), (i, x) -> even x)
selectPairs(3, toList(1..10), (i, x) -> even x)
Caveat
If @CODE "x"@ is a hash table, then it must be immutable.
SeeAlso
selectValues
selectKeys
Expand Down
3 changes: 3 additions & 0 deletions M2/Macaulay2/tests/normal/iterators.m2
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ assert Equation(toList applyPairs("foo", (i, c) -> (c, i + 1)),
x = 0
scanPairs("foo", (i, c) -> x += i + first ascii c)
assert Equation(x, 0 + 102 + 1 + 111 + 2 + 111)
assert Equation(selectPairs(1..10, (i, x) -> even x),
((1, 2), (3, 4), (5, 6), (7, 8), (9, 10)))
assert Equation(selectPairs(2, 1..10, (i, x) -> even x), ((1, 2), (3, 4)))

0 comments on commit 718cf4a

Please sign in to comment.