From 78a291ecaafa3439e6d947ace9f3ec5accba5e50 Mon Sep 17 00:00:00 2001 From: Chris Jefferson Date: Mon, 26 Sep 2016 21:27:00 +0100 Subject: [PATCH] Make ListX, SetX, SumX and ProductX support lists --- lib/coll.gi | 64 +++++++++++++-------------- tst/testinstall/xfuncs.tst | 88 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 32 deletions(-) create mode 100644 tst/testinstall/xfuncs.tst diff --git a/lib/coll.gi b/lib/coll.gi index 105ca3c18f..86275b1ae9 100644 --- a/lib/coll.gi +++ b/lib/coll.gi @@ -1758,7 +1758,7 @@ 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 ); @@ -1766,7 +1766,7 @@ ListXHelp := function ( result, gens, i, vals, l ) 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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -1872,7 +1872,7 @@ 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 ); @@ -1880,7 +1880,7 @@ SetXHelp := function ( result, gens, i, vals, l ) 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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -1986,7 +1986,7 @@ 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 ); @@ -1994,7 +1994,7 @@ SumXHelp := function ( result, gens, i, vals, l ) 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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -2120,7 +2120,7 @@ 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 ); @@ -2128,7 +2128,7 @@ ProductXHelp := function ( result, gens, i, vals, l ) 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; @@ -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; @@ -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; @@ -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; @@ -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; diff --git a/tst/testinstall/xfuncs.tst b/tst/testinstall/xfuncs.tst new file mode 100644 index 0000000000..595f9f2996 --- /dev/null +++ b/tst/testinstall/xfuncs.tst @@ -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);