Skip to content

Commit aed891a

Browse files
committed
WIP: add FirstX (TODO: add tests)
1 parent 274b3f5 commit aed891a

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

lib/coll.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2504,6 +2504,7 @@ DeclareGlobalFunction( "FoldLeftX" );
25042504
DeclareGlobalFunction( "ForAllX" );
25052505
DeclareGlobalFunction( "ForAnyX" );
25062506
DeclareGlobalFunction( "FilteredX" );
2507+
DeclareGlobalFunction( "FirstX" );
25072508
DeclareGlobalFunction( "NumberX" );
25082509
DeclareGlobalFunction( "PerformX" );
25092510

lib/coll.gi

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,7 +1849,7 @@ end );
18491849
InstallGlobalFunction( ForAllX, function ( arg )
18501850
local f;
18511851
f := Remove(arg);
1852-
return FoldLeftX(arg, {acc,x} -> acc and CallFuncList(f, x), true, false);
1852+
return FoldLeftX(arg, {acc,x} -> CallFuncList(f, x), true, false);
18531853
end );
18541854

18551855

@@ -1860,7 +1860,26 @@ end );
18601860
InstallGlobalFunction( ForAnyX, function ( arg )
18611861
local f;
18621862
f := Remove(arg);
1863-
return FoldLeftX(arg, {acc,x} -> acc or CallFuncList(f, x), false, true);
1863+
return FoldLeftX(arg, {acc,x} -> CallFuncList(f, x), false, true);
1864+
end );
1865+
1866+
1867+
#############################################################################
1868+
##
1869+
#M FirstX(<obj>,...)
1870+
##
1871+
InstallGlobalFunction( FirstX, function ( arg )
1872+
local f, result;
1873+
f := Remove(arg);
1874+
return FoldLeftX(arg,
1875+
function(acc, x)
1876+
if CallFuncList(f, x) then
1877+
result := x;
1878+
return true;
1879+
fi;
1880+
return false;
1881+
end, false, true);
1882+
return result;
18641883
end );
18651884

18661885

0 commit comments

Comments
 (0)