Skip to content

Commit 71e4e4e

Browse files
committed
Add ForAllX, ForAnyX, FilteredX, NumberX, PerformX
1 parent 7913ffa commit 71e4e4e

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-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: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,6 +1830,78 @@ 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+
# TODO: implement early abort
1852+
return FoldLeftX(arg, {acc,x} -> acc or CallFuncList(f, x), false, true);
1853+
end );
1854+
1855+
1856+
#############################################################################
1857+
##
1858+
#M FilteredX(<obj>,...)
1859+
##
1860+
InstallGlobalFunction( FilteredX, function ( arg )
1861+
local f;
1862+
f := Remove(arg);
1863+
return FoldLeftX(arg,
1864+
function(acc, x)
1865+
if CallFuncList(f, x) then
1866+
Add(acc, ShallowCopy(x));
1867+
fi;
1868+
return acc;
1869+
end, []);
1870+
end);
1871+
1872+
1873+
#############################################################################
1874+
##
1875+
#M NumberX(<obj>,...)
1876+
##
1877+
InstallGlobalFunction( NumberX, function ( arg )
1878+
local f;
1879+
f := Remove(arg);
1880+
return FoldLeftX(arg,
1881+
function(acc, x)
1882+
if CallFuncList(f, x) then
1883+
return acc + 1;
1884+
fi;
1885+
return acc;
1886+
end, 0);
1887+
end);
1888+
1889+
1890+
#############################################################################
1891+
##
1892+
#M PerformX(<obj>,...)
1893+
##
1894+
InstallGlobalFunction( PerformX, function ( arg )
1895+
local f;
1896+
f := Remove(arg);
1897+
FoldLeftX(arg,
1898+
function(acc, x)
1899+
CallFuncList(f, x);
1900+
return 0;
1901+
end, 0);
1902+
end);
1903+
1904+
18331905
#############################################################################
18341906
##
18351907
#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)