-
Notifications
You must be signed in to change notification settings - Fork 238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add several pairs-related methods #3617
Changes from all commits
1208f56
67f2ab5
6348b0c
5a6a333
2e43c71
a05d192
977dcd9
1c74996
7f4070b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,7 +104,7 @@ values(e:Expr):Expr := ( | |
else WrongArg("a hash table or dictionary")); | ||
setupfun("values",values); | ||
|
||
pairs(e:Expr):Expr := ( | ||
export pairs(e:Expr):Expr := ( | ||
when e | ||
is oc:DictionaryClosure do ( -- # typical value: pairs, Dictionary, List | ||
o := oc.dictionary.symboltable; | ||
|
@@ -142,8 +142,13 @@ pairs(e:Expr):Expr := ( | |
while i < length(o) do ( | ||
provide Expr(Sequence(toExpr(i),o.i)); | ||
i = i+1;))) | ||
is o:List do pairs(Expr(o.v)) -- # typical value: pairs, BasicList, List | ||
else WrongArg("a hash table, a sequence, a list, or a raw polynomial")); | ||
-- # typical value: pairs, BasicList, List | ||
is o:List do ( | ||
r := pairs(Expr(o.v)); | ||
when r | ||
is s:Sequence do list(o.Class, s, o.Mutable) | ||
else buildErrorPacket("internal error; expected a sequence")) | ||
else applyEE(getGlobalVariable(pairsIteratorS), e)); -- # typical value: pairs, Thing, Iterator | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fwiw I think putting the |
||
setupfun("pairs",pairs); | ||
|
||
-- operators | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
use evaluate; | ||
use actors; | ||
use actors2; | ||
use ballarith; | ||
|
||
isOption(e:Expr):bool := ( | ||
|
@@ -82,7 +83,7 @@ override(e:Expr):Expr := ( | |
if length(args) == 2 then ( | ||
when args.0 | ||
is h:HashTable do ( | ||
if h.Mutable then WrongArg("an immutable hash table") | ||
if h.Mutable then WrongArgImmutableHashTable() | ||
else when args.1 is v:Sequence do override(h,v,numOptions(v)) | ||
else override(h,Sequence(args.1),if isOption(args.1) then 1 else 0) | ||
) | ||
|
@@ -1895,6 +1896,22 @@ map(e:Expr):Expr := ( | |
else WrongNumArgs(2,3)); | ||
setupfun("apply",map); | ||
|
||
applyPairs(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 mappairs(a.1, o)) | ||
-- # typical value: applyPairs, BasicList, Function, List | ||
-- # typical value: applyPairs, Dictionary, Function, List | ||
-- # typical value: applyPairs, Thing, Function, Iterator | ||
else map(pairs(a.0), a.1)) | ||
else WrongNumArgs(2)) | ||
else WrongNumArgs(2)); | ||
setupfun("applyPairs", applyPairs); | ||
|
||
-- # typical value: scan, ZZ, Function, Thing | ||
scan(n:int,f:Expr):Expr := ( | ||
if n <= 0 then return nullE; | ||
|
@@ -2346,6 +2363,20 @@ scan(e:Expr):Expr := ( | |
else WrongNumArgs(2)); | ||
setupfun("scan",scan); | ||
|
||
-- # typical value: scanPairs, Thing, Function, Nothing | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this line is for readability of documentation and doesn't have to be literally true, I wonder if it would be useful to use a dummy |
||
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)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 := ( | ||
Comment on lines
+203
to
205
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think at some point we need to decide on a convention about where to place these lines. I don't find placing both of these here particularly useful. To me it either means one of them should be defined on the top level, or one of them is in the wrong place. |
||
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 WrongArg(1, "an immutable hash table") | ||
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, List | ||
else select(pairs(a.0), a.1)) | ||
else if length(a) == 3 then ( | ||
when a.0 | ||
is n:ZZcell do ( | ||
|
@@ -248,9 +250,10 @@ selectPairs(e:Expr):Expr := ( | |
when a.1 is obj:HashTable | ||
do ( | ||
if obj.Mutable | ||
then WrongArg(2, "an immutable hash table") | ||
then WrongArgImmutableHashTable(2) | ||
else selectPairs(toInt(n), obj, a.2)) | ||
else WrongArgHashTable(2))) | ||
-- # typical value: selectPairs, ZZ, BasicList, Function, List | ||
else select(a.0, pairs(a.1), a.2, nullE, nullE))) | ||
else WrongArgZZ(1)) | ||
else WrongNumArgs(2, 3)) | ||
else WrongNumArgs(2, 3)); | ||
|
@@ -298,7 +301,7 @@ any(f:Expr,e:Expr):Expr := ( | |
is b:List do Expr(any(f,b.v)) | ||
is i:ZZcell do if isInt(i) then Expr(any(f,toInt(i))) else WrongArgSmallInteger(1) | ||
is c:HashTable do | ||
if c.Mutable then WrongArg(1,"an immutable hash table") else | ||
if c.Mutable then WrongArgImmutableHashTable(1) else | ||
Expr(any(f,c)) | ||
else WrongArg("a list or a hash table")); | ||
any(f:Expr,a:Sequence,b:Sequence):Expr := ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -389,6 +389,8 @@ export WrongArgMatrix(n:int):Expr := WrongArg(n,"a raw matrix"); | |
export WrongArgMatrix():Expr := WrongArg("a raw matrix"); | ||
export WrongArgHashTable():Expr := WrongArg("a hash table"); | ||
export WrongArgHashTable(n:int):Expr := WrongArg(n, "a hash table"); | ||
export WrongArgImmutableHashTable():Expr := WrongArg("an immutable hash table"); | ||
export WrongArgImmutableHashTable(n:int):Expr := WrongArg(n, "an immutable hash table"); | ||
Comment on lines
390
to
+393
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems to me like |
||
export ArgChanged(name:string,n:int):Expr := ( | ||
buildErrorPacket(quoteit(name) + " expected argument " + tostring(n) | ||
+ " not to change its type during execution")); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when would
pairs(Expr(o.v))
witho:List
fail?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It shouldn't -- we should always get a sequence here. I don't expect we'll ever raise this error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, unless
pairs
raises an error... I'll look into this more.