Skip to content

Commit

Permalink
Add scanPairs for non-hash tables
Browse files Browse the repository at this point in the history
scanPairs(x, f) is the same as scan(pairs x, f)
  • Loading branch information
d-torrance committed Dec 18, 2024
1 parent 86914d6 commit 88ab651
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
14 changes: 14 additions & 0 deletions M2/Macaulay2/d/actors3.d
Original file line number Diff line number Diff line change
Expand Up @@ -2363,6 +2363,20 @@ scan(e:Expr):Expr := (
else WrongNumArgs(2));
setupfun("scan",scan);

-- # typical value: scanPairs, Thing, Function, Nothing
scanPairs(e:Expr):Expr := (
when e
is a:Sequence do (
if length(a) == 2 then (
when a.0
is o:HashTable do (
if o.Mutable then WrongArgImmutableHashTable(1)
else scanpairs(a.1, o))
else scan(pairs(a.0), a.1))
else WrongNumArgs(2))
else WrongNumArgs(2));
setupfun("scanPairs", scanPairs);

nextPrime(e:Expr):Expr := (
when e
is x:ZZcell do toExpr(nextPrime(x.v - oneZZ))
Expand Down
14 changes: 1 addition & 13 deletions M2/Macaulay2/d/evaluate.d
Original file line number Diff line number Diff line change
Expand Up @@ -1710,7 +1710,7 @@ setup(LeftArrowS,assigntofun);
idfun(e:Expr):Expr := e;
setupfun("identity",idfun);
-- # typical value: scanPairs, HashTable, Function, Nothing
scanpairs(f:Expr,obj:HashTable):Expr := ( -- obj is not Mutable
export scanpairs(f:Expr,obj:HashTable):Expr := ( -- obj is not Mutable
foreach bucket in obj.table do (
p := bucket;
while true do (
Expand All @@ -1720,18 +1720,6 @@ scanpairs(f:Expr,obj:HashTable):Expr := ( -- obj is not Mutable
p = p.next;
));
nullE);
scanpairsfun(e:Expr):Expr := (
when e is a:Sequence do
if length(a) == 2
then when a.0 is o:HashTable
do
if o.Mutable
then WrongArgImmutableHashTable()
else scanpairs(a.1,o)
else WrongArgHashTable(1)
else WrongNumArgs(2)
else WrongNumArgs(2));
setupfun("scanPairs",scanpairsfun);

mpre():Expr := buildErrorPacket("applyPairs: expected function to return null, a sequence of length 2, or an option x=>y");
-- # typical value: applyPairs, HashTable, Function, HashTable
Expand Down
11 changes: 9 additions & 2 deletions M2/Macaulay2/packages/Macaulay2Doc/doc_tables.m2
Original file line number Diff line number Diff line change
Expand Up @@ -388,12 +388,14 @@ doc ///
Key
scanPairs
(scanPairs, HashTable, Function)
(scanPairs, Thing, Function)
Headline
apply a function to the pairs in a hash table
Usage
scanPairs(t, f)
Inputs
t:HashTable
t:{HashTable, BasicList, Dictionary}
or any instance of a class with an @TO iterator@ method installed
f:Function
Description
Text
Expand All @@ -404,7 +406,12 @@ doc ///
Example
t = hashTable {{1,8},{2,20},{3,4},{4,20}}
scanPairs(t, (k,v) -> print (k+v))
scanPairs(t, (k,v) -> if v==20 then print k)
scanPairs(t, (k,v) -> if v==20 then print k)
Text
If @CODE "t"@ is not a hash table, then @M2CODE "scan(pairs t, f)"@ is
called.
Example
scanPairs({4, 5, 6}, print)
Caveat
This function requires an immutable hash table. To scan the pairs in
a mutable hash table, use {\tt scan(pairs t, f)}.
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 @@ -56,3 +56,6 @@ assert Equation(join([1, 2, 3], iterator {4, 5, 6}), [1, 2, 3, 4, 5, 6])
assert Equation(toList pairs "foo", {(0, "f"), (1, "o"), (2, "o")})
assert Equation(toList applyPairs("foo", (i, c) -> (c, i + 1)),
{("f", 1), ("o", 2), ("o", 3)})
x = 0
scanPairs("foo", (i, c) -> x += i + first ascii c)
assert Equation(x, 0 + 102 + 1 + 111 + 2 + 111)

0 comments on commit 88ab651

Please sign in to comment.