1
0
Fork 0

Make std.algorithm conditions more efficient

This commit is contained in:
TheFGFSEagle 2023-02-03 23:16:35 +01:00 committed by James Turner
parent a5f2e19c12
commit 00efa2d01a

View file

@ -7,14 +7,22 @@ var all = func(obj, key=nil) {
return 0;
}
var res = 1;
foreach (var o; obj) {
if (key) {
res &= key(o);
} else {
res &= o;
if (key) {
if (!isfunc(key)) {
die("std.all got a non-callable 'key' argument");
}
if (res == 0) {
break;
foreach (var o; obj) {
res &= key(o);
if (res == 0) {
break;
}
}
} else {
foreach(var o; obj) {
res &= o;
if (res == 0) {
break;
}
}
}
return res;
@ -22,37 +30,55 @@ var all = func(obj, key=nil) {
var any = func(obj, key=nil) {
var res = 0;
foreach (var o; obj) {
if (key) {
if (key) {
if (!isfunc(key)) {
die("std.any got a non-callable 'key' argument");
}
foreach (var o; obj) {
res |= key(o);
} else {
if (res == 1) {
break;
}
}
} else {
foreach(var o; obj) {
res |= o;
}
if (res == 1) {
break;
if (res == 1) {
break;
}
}
}
return res;
};
var map = func(f, obj) {
var map = func(function, obj) {
if (!isfunc(function)) {
die("std.map got a non-callable 'function' argument");
}
var res = [];
foreach (var o; obj) {
append(res, f(o));
append(res, function(o));
}
return res;
};
var filter = func(f, obj) {
var filter = func(function, obj) {
var res = [];
foreach (var o; obj) {
if (f) {
if (f(o)) {
if (function) {
if (!isfunc(function)) {
die("std.filter got a non-callable 'function' argument");
}
foreach (var o; obj) {
if (function(o)) {
append(res, o);
}
}
} elsif (o) {
foreach (var o; obj) {
if (o) {
append(res, o);
}
} elsif (o) {
append(res, o);
}
}
return res;
};
};