Skip to content

Commit 102aa97

Browse files
committed
Add ForAllX, ForAnyX, FilteredX, NumberX, PerformX
1 parent bfb334b commit 102aa97

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
@@ -2423,6 +2423,17 @@ DeclareOperation( "FoldLeftOp", [ IsListOrCollection, IsFunction ] );
24232423
DeclareGlobalFunction( "FoldLeftX" );
24242424

24252425

2426+
#############################################################################
2427+
##
2428+
## TODO: document the following
2429+
##
2430+
DeclareGlobalFunction( "ForAllX" );
2431+
DeclareGlobalFunction( "ForAnyX" );
2432+
DeclareGlobalFunction( "FilteredX" );
2433+
DeclareGlobalFunction( "NumberX" );
2434+
DeclareGlobalFunction( "PerformX" );
2435+
2436+
24262437
#############################################################################
24272438
##
24282439
#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
@@ -1843,6 +1843,77 @@ InstallGlobalFunction( ProductX, function ( arg )
18431843
end );
18441844

18451845

1846+
#############################################################################
1847+
##
1848+
#M ForAllX(<obj>,...)
1849+
##
1850+
InstallGlobalFunction( ForAllX, function ( arg )
1851+
local f;
1852+
f := Remove(arg);
1853+
return FoldLeftX(arg, {acc,x} -> acc and CallFuncList(f, x), true, false);
1854+
end );
1855+
1856+
1857+
#############################################################################
1858+
##
1859+
#M ForAnyX(<obj>,...)
1860+
##
1861+
InstallGlobalFunction( ForAnyX, function ( arg )
1862+
local f;
1863+
f := Remove(arg);
1864+
return FoldLeftX(arg, {acc,x} -> acc or CallFuncList(f, x), false, true);
1865+
end );
1866+
1867+
1868+
#############################################################################
1869+
##
1870+
#M FilteredX(<obj>,...)
1871+
##
1872+
InstallGlobalFunction( FilteredX, function ( arg )
1873+
local f;
1874+
f := Remove(arg);
1875+
return FoldLeftX(arg,
1876+
function(acc, x)
1877+
if CallFuncList(f, x) then
1878+
Add(acc, ShallowCopy(x));
1879+
fi;
1880+
return acc;
1881+
end, []);
1882+
end);
1883+
1884+
1885+
#############################################################################
1886+
##
1887+
#M NumberX(<obj>,...)
1888+
##
1889+
InstallGlobalFunction( NumberX, function ( arg )
1890+
local f;
1891+
f := Remove(arg);
1892+
return FoldLeftX(arg,
1893+
function(acc, x)
1894+
if CallFuncList(f, x) then
1895+
return acc + 1;
1896+
fi;
1897+
return acc;
1898+
end, 0);
1899+
end);
1900+
1901+
1902+
#############################################################################
1903+
##
1904+
#M PerformX(<obj>,...)
1905+
##
1906+
InstallGlobalFunction( PerformX, function ( arg )
1907+
local f;
1908+
f := Remove(arg);
1909+
FoldLeftX(arg,
1910+
function(acc, x)
1911+
CallFuncList(f, x);
1912+
return 0;
1913+
end, 0);
1914+
end);
1915+
1916+
18461917
#############################################################################
18471918
##
18481919
#F Perform( <list>, <func> )

tst/testinstall/coll.tst

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,57 @@ gap> FoldLeft([1..3], \-);
330330
gap> FoldLeft([1..3], \-, 0);
331331
-6
332332
333+
#############################################################################
334+
#
335+
# ForAllX
336+
#
337+
gap> ForAllX([1..2], [3..4], function(i,j) Display([i,j]); return true; end);
338+
[ 1, 3 ]
339+
[ 1, 4 ]
340+
[ 2, 3 ]
341+
[ 2, 4 ]
342+
true
343+
gap> # verify short circuit works
344+
gap> ForAllX([1..2], [3..4], function(i,j) Display([i,j]); return false; end);
345+
[ 1, 3 ]
346+
false
347+
348+
#############################################################################
349+
#
350+
# ForAnyX
351+
#
352+
gap> ForAnyX([1..2], [3..4], function(i,j) Display([i,j]); return false; end);
353+
[ 1, 3 ]
354+
[ 1, 4 ]
355+
[ 2, 3 ]
356+
[ 2, 4 ]
357+
false
358+
gap> # verify short circuit works
359+
gap> ForAnyX([1..2], [3..4], function(i,j) Display([i,j]); return true; end);
360+
[ 1, 3 ]
361+
true
362+
363+
#############################################################################
364+
#
365+
# FilteredX
366+
#
367+
gap> FilteredX([1..2], [3..4], ReturnTrue);
368+
[ [ 1, 3 ], [ 1, 4 ], [ 2, 3 ], [ 2, 4 ] ]
369+
370+
#############################################################################
371+
#
372+
# NumberX
373+
#
374+
gap> NumberX([1..2], [3..4], ReturnTrue);
375+
4
376+
377+
#############################################################################
378+
#
379+
# PerformX
380+
#
381+
gap> PerformX([1..2], [3..4], Print); Print("\n");
382+
13142324
383+
333384
#############################################################################
334385
#
335386
# List

0 commit comments

Comments
 (0)