Skip to content

Commit ab68951

Browse files
committed
Add ForAllX, ForAnyX, FilteredX, NumberX, PerformX
1 parent 6cb1219 commit ab68951

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

lib/coll.gd

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,6 +2421,17 @@ DeclareOperation( "FoldLeftOp", [ IsListOrCollection, IsFunction ] );
24212421
DeclareGlobalFunction( "FoldLeftX" );
24222422

24232423

2424+
#############################################################################
2425+
##
2426+
## TODO: document the following
2427+
##
2428+
DeclareGlobalFunction( "ForAllX" );
2429+
DeclareGlobalFunction( "ForAnyX" );
2430+
DeclareGlobalFunction( "FilteredX" );
2431+
DeclareGlobalFunction( "NumberX" );
2432+
DeclareGlobalFunction( "PerformX" );
2433+
2434+
24242435
#############################################################################
24252436
##
24262437
#F Sum( <list>[, <init>] ) . . . . . . . . . . sum of the elements of a list

lib/coll.gi

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,6 +1830,77 @@ InstallGlobalFunction( ProductX, function ( arg )
18301830
end );
18311831

18321832

1833+
#############################################################################
1834+
##
1835+
#M ForAllX(<obj>,...)
1836+
##
1837+
InstallGlobalFunction( ForAllX, function ( arg )
1838+
local f;
1839+
f := Remove(arg);
1840+
return FoldLeftX(arg, {acc,x} -> acc and CallFuncList(f, x), true, false);
1841+
end );
1842+
1843+
1844+
#############################################################################
1845+
##
1846+
#M ForAnyX(<obj>,...)
1847+
##
1848+
InstallGlobalFunction( ForAnyX, function ( arg )
1849+
local f;
1850+
f := Remove(arg);
1851+
return FoldLeftX(arg, {acc,x} -> acc or CallFuncList(f, x), false, true);
1852+
end );
1853+
1854+
1855+
#############################################################################
1856+
##
1857+
#M FilteredX(<obj>,...)
1858+
##
1859+
InstallGlobalFunction( FilteredX, function ( arg )
1860+
local f;
1861+
f := Remove(arg);
1862+
return FoldLeftX(arg,
1863+
function(acc, x)
1864+
if CallFuncList(f, x) then
1865+
Add(acc, ShallowCopy(x));
1866+
fi;
1867+
return acc;
1868+
end, []);
1869+
end);
1870+
1871+
1872+
#############################################################################
1873+
##
1874+
#M NumberX(<obj>,...)
1875+
##
1876+
InstallGlobalFunction( NumberX, function ( arg )
1877+
local f;
1878+
f := Remove(arg);
1879+
return FoldLeftX(arg,
1880+
function(acc, x)
1881+
if CallFuncList(f, x) then
1882+
return acc + 1;
1883+
fi;
1884+
return acc;
1885+
end, 0);
1886+
end);
1887+
1888+
1889+
#############################################################################
1890+
##
1891+
#M PerformX(<obj>,...)
1892+
##
1893+
InstallGlobalFunction( PerformX, function ( arg )
1894+
local f;
1895+
f := Remove(arg);
1896+
FoldLeftX(arg,
1897+
function(acc, x)
1898+
CallFuncList(f, x);
1899+
return 0;
1900+
end, 0);
1901+
end);
1902+
1903+
18331904
#############################################################################
18341905
##
18351906
#F Perform( <list>, <func> )

tst/testinstall/coll.tst

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,57 @@ gap> FoldLeft([1..3], \-);
323323
gap> FoldLeft([1..3], \-, 0);
324324
-6
325325
326+
#############################################################################
327+
#
328+
# ForAllX
329+
#
330+
gap> ForAllX([1..2], [3..4], function(i,j) Display([i,j]); return true; end);
331+
[ 1, 3 ]
332+
[ 1, 4 ]
333+
[ 2, 3 ]
334+
[ 2, 4 ]
335+
true
336+
gap> # verify short circuit works
337+
gap> ForAllX([1..2], [3..4], function(i,j) Display([i,j]); return false; end);
338+
[ 1, 3 ]
339+
false
340+
341+
#############################################################################
342+
#
343+
# ForAnyX
344+
#
345+
gap> ForAnyX([1..2], [3..4], function(i,j) Display([i,j]); return false; end);
346+
[ 1, 3 ]
347+
[ 1, 4 ]
348+
[ 2, 3 ]
349+
[ 2, 4 ]
350+
false
351+
gap> # verify short circuit works
352+
gap> ForAnyX([1..2], [3..4], function(i,j) Display([i,j]); return true; end);
353+
[ 1, 3 ]
354+
true
355+
356+
#############################################################################
357+
#
358+
# FilteredX
359+
#
360+
gap> FilteredX([1..2], [3..4], ReturnTrue);
361+
[ [ 1, 3 ], [ 1, 4 ], [ 2, 3 ], [ 2, 4 ] ]
362+
363+
#############################################################################
364+
#
365+
# NumberX
366+
#
367+
gap> NumberX([1..2], [3..4], ReturnTrue);
368+
4
369+
370+
#############################################################################
371+
#
372+
# PerformX
373+
#
374+
gap> PerformX([1..2], [3..4], Print); Print("\n");
375+
13142324
376+
326377
#############################################################################
327378
#
328379
# List

0 commit comments

Comments
 (0)