diff --git a/M2/Macaulay2/d/actors4.d b/M2/Macaulay2/d/actors4.d index 71e3aff195..3f6ae4605d 100644 --- a/M2/Macaulay2/d/actors4.d +++ b/M2/Macaulay2/d/actors4.d @@ -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; @@ -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 ( @@ -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)); diff --git a/M2/Macaulay2/packages/Macaulay2Doc/functions/select-doc.m2 b/M2/Macaulay2/packages/Macaulay2Doc/functions/select-doc.m2 index 6f4ea9b5cc..8132129732 100644 --- a/M2/Macaulay2/packages/Macaulay2Doc/functions/select-doc.m2 +++ b/M2/Macaulay2/packages/Macaulay2Doc/functions/select-doc.m2 @@ -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 @@ -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. @@ -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 diff --git a/M2/Macaulay2/tests/normal/iterators.m2 b/M2/Macaulay2/tests/normal/iterators.m2 index f9d4ad5afa..340e14c4aa 100644 --- a/M2/Macaulay2/tests/normal/iterators.m2 +++ b/M2/Macaulay2/tests/normal/iterators.m2 @@ -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)))