2023-01-18 14:27:58 +00:00
|
|
|
|
|
|
|
# SPDX-FileCopyrightText: (C) 2023 Frederic Croix <thefgfseagle@gmail.com>
|
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
|
|
|
var all = func(obj, key=nil) {
|
|
|
|
if (!size(obj)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
var res = 1;
|
2023-02-03 22:16:35 +00:00
|
|
|
if (key) {
|
|
|
|
if (!isfunc(key)) {
|
|
|
|
die("std.all got a non-callable 'key' argument");
|
|
|
|
}
|
|
|
|
foreach (var o; obj) {
|
2023-01-18 14:27:58 +00:00
|
|
|
res &= key(o);
|
2023-02-03 22:16:35 +00:00
|
|
|
if (res == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2023-01-18 14:27:58 +00:00
|
|
|
}
|
2023-02-03 22:16:35 +00:00
|
|
|
} else {
|
|
|
|
foreach(var o; obj) {
|
|
|
|
res &= o;
|
|
|
|
if (res == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2023-01-18 14:27:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
|
|
|
var any = func(obj, key=nil) {
|
|
|
|
var res = 0;
|
2023-02-03 22:16:35 +00:00
|
|
|
if (key) {
|
|
|
|
if (!isfunc(key)) {
|
|
|
|
die("std.any got a non-callable 'key' argument");
|
|
|
|
}
|
|
|
|
foreach (var o; obj) {
|
2023-01-18 14:27:58 +00:00
|
|
|
res |= key(o);
|
2023-02-03 22:16:35 +00:00
|
|
|
if (res == 1) {
|
|
|
|
break;
|
|
|
|
}
|
2023-01-18 14:27:58 +00:00
|
|
|
}
|
2023-02-03 22:16:35 +00:00
|
|
|
} else {
|
|
|
|
foreach(var o; obj) {
|
|
|
|
res |= o;
|
|
|
|
if (res == 1) {
|
|
|
|
break;
|
|
|
|
}
|
2023-01-18 14:27:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
2023-02-03 22:16:35 +00:00
|
|
|
var map = func(function, obj) {
|
|
|
|
if (!isfunc(function)) {
|
|
|
|
die("std.map got a non-callable 'function' argument");
|
|
|
|
}
|
2023-01-18 14:27:58 +00:00
|
|
|
var res = [];
|
|
|
|
foreach (var o; obj) {
|
2023-02-03 22:16:35 +00:00
|
|
|
append(res, function(o));
|
2023-01-18 14:27:58 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
2023-02-03 22:16:35 +00:00
|
|
|
var filter = func(function, obj) {
|
2023-01-18 14:27:58 +00:00
|
|
|
var res = [];
|
2023-02-03 22:16:35 +00:00
|
|
|
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) {
|
2023-01-18 14:27:58 +00:00
|
|
|
append(res, o);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
2023-02-03 22:16:35 +00:00
|
|
|
};
|