Skip to content

Commit

Permalink
Merge PR #903: Make ListX, SetX, SumX and ProductX support lists
Browse files Browse the repository at this point in the history
These functions are documented to take both lists and collections, but the code
actually only accepted collections.

Fixes #902
  • Loading branch information
fingolfin authored Sep 28, 2016
2 parents d60d5c3 + 78a291e commit b6874cf
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 32 deletions.
64 changes: 32 additions & 32 deletions lib/coll.gi
Original file line number Diff line number Diff line change
Expand Up @@ -1758,15 +1758,15 @@ ListXHelp := function ( result, gens, i, vals, l )
i := i + 1;
elif gen = false then
return;
elif IsCollection( gen ) then
elif IsListOrCollection( gen ) then
for val in gen do
vals[l+1] := val;
ListXHelp( result, gens, i+1, vals, l+1 );
od;
Unbind( vals[l+1] );
return;
else
Error( "gens[",i+1,"] must be a collection, a boolean, ",
Error( "gens[",i+1,"] must be a collection, a list, a boolean, ",
"or a function" );
fi;
od;
Expand All @@ -1785,7 +1785,7 @@ BIND_GLOBAL( "ListXHelp2", function ( result, gens, i, val1, val2 )
i := i + 1;
elif gen = false then
return;
elif IsCollection( gen ) then
elif IsListOrCollection( gen ) then
vals := [ val1, val2 ];
for val3 in gen do
vals[3] := val3;
Expand All @@ -1794,7 +1794,7 @@ BIND_GLOBAL( "ListXHelp2", function ( result, gens, i, val1, val2 )
Unbind( vals[3] );
return;
else
Error( "gens[",i+1,"] must be a collection, a boolean, ",
Error( "gens[",i+1,"] must be a collection, a list, a boolean, ",
"or a function" );
fi;
od;
Expand All @@ -1812,13 +1812,13 @@ BIND_GLOBAL( "ListXHelp1", function ( result, gens, i, val1 )
i := i + 1;
elif gen = false then
return;
elif IsCollection( gen ) then
elif IsListOrCollection( gen ) then
for val2 in gen do
ListXHelp2( result, gens, i+1, val1, val2 );
od;
return;
else
Error( "gens[",i+1,"] must be a collection, a boolean, ",
Error( "gens[",i+1,"] must be a collection, a list, a boolean, ",
"or a function" );
fi;
od;
Expand All @@ -1836,13 +1836,13 @@ BIND_GLOBAL( "ListXHelp0", function ( result, gens, i )
i := i + 1;
elif gen = false then
return;
elif IsCollection( gen ) then
elif IsListOrCollection( gen ) then
for val1 in gen do
ListXHelp1( result, gens, i+1, val1 );
od;
return;
else
Error( "gens[",i+1,"] must be a collection, a boolean, ",
Error( "gens[",i+1,"] must be a collection, a list, a boolean, ",
"or a function" );
fi;
od;
Expand Down Expand Up @@ -1872,15 +1872,15 @@ SetXHelp := function ( result, gens, i, vals, l )
i := i + 1;
elif gen = false then
return;
elif IsCollection( gen ) then
elif IsListOrCollection( gen ) then
for val in gen do
vals[l+1] := val;
SetXHelp( result, gens, i+1, vals, l+1 );
od;
Unbind( vals[l+1] );
return;
else
Error( "gens[",i+1,"] must be a collection, a boolean, ",
Error( "gens[",i+1,"] must be a collection, a list, a boolean, ",
"or a function" );
fi;
od;
Expand All @@ -1899,7 +1899,7 @@ BIND_GLOBAL( "SetXHelp2", function ( result, gens, i, val1, val2 )
i := i + 1;
elif gen = false then
return;
elif IsCollection( gen ) then
elif IsListOrCollection( gen ) then
vals := [ val1, val2 ];
for val3 in gen do
vals[3] := val3;
Expand All @@ -1908,7 +1908,7 @@ BIND_GLOBAL( "SetXHelp2", function ( result, gens, i, val1, val2 )
Unbind( vals[3] );
return;
else
Error( "gens[",i+1,"] must be a collection, a boolean, ",
Error( "gens[",i+1,"] must be a collection, a list, a boolean, ",
"or a function" );
fi;
od;
Expand All @@ -1926,13 +1926,13 @@ BIND_GLOBAL( "SetXHelp1", function ( result, gens, i, val1 )
i := i + 1;
elif gen = false then
return;
elif IsCollection( gen ) then
elif IsListOrCollection( gen ) then
for val2 in gen do
SetXHelp2( result, gens, i+1, val1, val2 );
od;
return;
else
Error( "gens[",i+1,"] must be a collection, a boolean, ",
Error( "gens[",i+1,"] must be a collection, a list, a boolean, ",
"or a function" );
fi;
od;
Expand All @@ -1950,13 +1950,13 @@ BIND_GLOBAL( "SetXHelp0", function ( result, gens, i )
i := i + 1;
elif gen = false then
return;
elif IsCollection( gen ) then
elif IsListOrCollection( gen ) then
for val1 in gen do
SetXHelp1( result, gens, i+1, val1 );
od;
return;
else
Error( "gens[",i+1,"] must be a collection, a boolean, ",
Error( "gens[",i+1,"] must be a collection, a list, a boolean, ",
"or a function" );
fi;
od;
Expand Down Expand Up @@ -1986,15 +1986,15 @@ SumXHelp := function ( result, gens, i, vals, l )
i := i + 1;
elif gen = false then
return result;
elif IsCollection( gen ) then
elif IsListOrCollection( gen ) then
for val in gen do
vals[l+1] := val;
result := SumXHelp( result, gens, i+1, vals, l+1 );
od;
Unbind( vals[l+1] );
return result;
else
Error( "gens[",i+1,"] must be a collection, a boolean, ",
Error( "gens[",i+1,"] must be a collection, a list, a boolean, ",
"or a function" );
fi;
od;
Expand All @@ -2018,7 +2018,7 @@ BIND_GLOBAL( "SumXHelp2", function ( result, gens, i, val1, val2 )
i := i + 1;
elif gen = false then
return result;
elif IsCollection( gen ) then
elif IsListOrCollection( gen ) then
vals := [ val1, val2 ];
for val3 in gen do
vals[3] := val3;
Expand All @@ -2027,7 +2027,7 @@ BIND_GLOBAL( "SumXHelp2", function ( result, gens, i, val1, val2 )
Unbind( vals[3] );
return result;
else
Error( "gens[",i+1,"] must be a collection, a boolean, ",
Error( "gens[",i+1,"] must be a collection, a list, a boolean, ",
"or a function" );
fi;
od;
Expand All @@ -2050,13 +2050,13 @@ BIND_GLOBAL( "SumXHelp1", function ( result, gens, i, val1 )
i := i + 1;
elif gen = false then
return result;
elif IsCollection( gen ) then
elif IsListOrCollection( gen ) then
for val2 in gen do
result := SumXHelp2( result, gens, i+1, val1, val2 );
od;
return result;
else
Error( "gens[",i+1,"] must be a collection, a boolean, ",
Error( "gens[",i+1,"] must be a collection, a list, a boolean, ",
"or a function" );
fi;
od;
Expand All @@ -2079,13 +2079,13 @@ BIND_GLOBAL( "SumXHelp0", function ( result, gens, i )
i := i + 1;
elif gen = false then
return result;
elif IsCollection( gen ) then
elif IsListOrCollection( gen ) then
for val1 in gen do
result := SumXHelp1( result, gens, i+1, val1 );
od;
return result;
else
Error( "gens[",i+1,"] must be a collection, a boolean, ",
Error( "gens[",i+1,"] must be a collection, a list, a boolean, ",
"or a function" );
fi;
od;
Expand Down Expand Up @@ -2120,15 +2120,15 @@ ProductXHelp := function ( result, gens, i, vals, l )
i := i + 1;
elif gen = false then
return result;
elif IsCollection( gen ) then
elif IsListOrCollection( gen ) then
for val in gen do
vals[l+1] := val;
result := ProductXHelp( result, gens, i+1, vals, l+1 );
od;
Unbind( vals[l+1] );
return result;
else
Error( "gens[",i+1,"] must be a collection, a boolean, ",
Error( "gens[",i+1,"] must be a collection, a list, a boolean, ",
"or a function" );
fi;
od;
Expand All @@ -2152,7 +2152,7 @@ BIND_GLOBAL( "ProductXHelp2", function ( result, gens, i, val1, val2 )
i := i + 1;
elif gen = false then
return result;
elif IsCollection( gen ) then
elif IsListOrCollection( gen ) then
vals := [ val1, val2 ];
for val3 in gen do
vals[3] := val3;
Expand All @@ -2161,7 +2161,7 @@ BIND_GLOBAL( "ProductXHelp2", function ( result, gens, i, val1, val2 )
Unbind( vals[3] );
return result;
else
Error( "gens[",i+1,"] must be a collection, a boolean, ",
Error( "gens[",i+1,"] must be a collection, a list, a boolean, ",
"or a function" );
fi;
od;
Expand All @@ -2184,13 +2184,13 @@ BIND_GLOBAL( "ProductXHelp1", function ( result, gens, i, val1 )
i := i + 1;
elif gen = false then
return result;
elif IsCollection( gen ) then
elif IsListOrCollection( gen ) then
for val2 in gen do
result := ProductXHelp2( result, gens, i+1, val1, val2 );
od;
return result;
else
Error( "gens[",i+1,"] must be a collection, a boolean, ",
Error( "gens[",i+1,"] must be a collection, a list, a boolean, ",
"or a function" );
fi;
od;
Expand All @@ -2213,13 +2213,13 @@ BIND_GLOBAL( "ProductXHelp0", function ( result, gens, i )
i := i + 1;
elif gen = false then
return result;
elif IsCollection( gen ) then
elif IsListOrCollection( gen ) then
for val1 in gen do
result := ProductXHelp1( result, gens, i+1, val1 );
od;
return result;
else
Error( "gens[",i+1,"] must be a collection, a boolean, ",
Error( "gens[",i+1,"] must be a collection, a list, a boolean, ",
"or a function" );
fi;
od;
Expand Down
88 changes: 88 additions & 0 deletions tst/testinstall/xfuncs.tst
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
gap> START_TEST("xfuncs.tst");
gap> testXfuncs := function(list, args...)
> local set, sum, prod;
> list := CallFuncList(ListX, args);
> set := Set(list);
> if IsEmpty(list) then
> sum := fail; prod := fail;
> else
> sum := Sum(list); prod := Product(list);
> fi;
> return list = CallFuncList(ListX, args) and
> set = CallFuncList(SetX, args) and
> prod = CallFuncList(ProductX, args) and
> sum = CallFuncList(SumX, args);
> end;;
gap> testXfuncs([3], function() return 3; end);
true
gap> testXfuncs([2], [2], x -> x);
true
gap> testXfuncs([2,3], [2,3], x -> x);
true
gap> testXfuncs([2,3,2], [2,3,2], x -> x);
true
gap> testXfuncs([ [1,1], [1,2], [1,3], [2,1], [2,2], [2,3] ],
> [1,2], [1,2,3], function(x,y) return [x,y]; end);
true
gap> testXfuncs([ [ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ], [ 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] ], [1..2],[1],[2],[3],[4],[5],[6],[7],[8],[9],[10], function(x...) return x; end);
true
gap> sumlim := function(x...) return Sum(x) < 5; end;;
gap> testXfuncs([ [ 1, 1, 1, 1 ] ], [1..5], sumlim, [1..5], sumlim, sumlim, [1..5], [1..5], sumlim, function(x...) return x; end);
true
gap> testXfuncs([ [ 1 ], [ 2 ] ],[1..2], ReturnTrue, function(x...) return x; end);
true
gap> testXfuncs([], [1..2], ReturnFalse, function(x...) return x; end);
true
gap> testXfuncs([ [1],[2] ],function() return true; end, [1..2], function(x...) return x; end);
true
gap> testXfuncs([], function() return false; end, [1..2], function(x...) return x; end);
true
gap> testXfuncs([1,3,5],[1..5], x -> (x mod 2 = 1), x -> x);
true
gap> testXfuncs([ [1,3] ],[1..5], x -> (x mod 2 = 1), [3..5], function(x,y) return x + y < 5; end, function(x...) return x; end);
true
gap> testXfuncs([], [], function(x,y) return [x,y]; end);
true
gap> testXfuncs([], [1,2,3], [], x -> x);
true
gap> ListX([1, "abc", "abc"], x -> x);
[ 1, "abc", "abc" ]
gap> SetX([1, "abc", "abc"], x -> x);
[ 1, "abc" ]
gap> SumX([1, 2, 2.0], x -> x);
5.
gap> ProductX([1, 2, 2.0], x -> x);
4.
gap> ListX( (1,2), x -> x);
Error, gens[1] must be a collection, a list, a boolean, or a function
gap> SetX( (1,2), x -> x);
Error, gens[1] must be a collection, a list, a boolean, or a function
gap> SumX( (1,2), x -> x);
Error, gens[1] must be a collection, a list, a boolean, or a function
gap> ProductX( (1,2), x -> x);
Error, gens[1] must be a collection, a list, a boolean, or a function
gap> ListX([1,2], (1,2), x -> x);
Error, gens[2] must be a collection, a list, a boolean, or a function
gap> SetX([1,2], (1,2), x -> x);
Error, gens[2] must be a collection, a list, a boolean, or a function
gap> SumX([1,2], (1,2), x -> x);
Error, gens[2] must be a collection, a list, a boolean, or a function
gap> ProductX([1,2], (1,2), x -> x);
Error, gens[2] must be a collection, a list, a boolean, or a function
gap> ListX([1,2],[1,2], (1,2), x -> x);
Error, gens[3] must be a collection, a list, a boolean, or a function
gap> SetX([1,2],[1,2], (1,2), x -> x);
Error, gens[3] must be a collection, a list, a boolean, or a function
gap> SumX([1,2],[1,2], (1,2), x -> x);
Error, gens[3] must be a collection, a list, a boolean, or a function
gap> ProductX([1,2],[1,2], (1,2), x -> x);
Error, gens[3] must be a collection, a list, a boolean, or a function
gap> ListX([1,2],[1,2],[1,2], (1,2), x -> x);
Error, gens[4] must be a collection, a list, a boolean, or a function
gap> SetX([1,2],[1,2],[1,2], (1,2), x -> x);
Error, gens[4] must be a collection, a list, a boolean, or a function
gap> SumX([1,2],[1,2],[1,2], (1,2), x -> x);
Error, gens[4] must be a collection, a list, a boolean, or a function
gap> ProductX([1,2],[1,2],[1,2], (1,2), x -> x);
Error, gens[4] must be a collection, a list, a boolean, or a function
gap> STOP_TEST("xfuncs.tst", 1);

0 comments on commit b6874cf

Please sign in to comment.