1
0
Fork 0

Added some utility functions (all, any, map and filter)

This commit is contained in:
TheFGFSEagle 2023-01-18 15:27:58 +01:00 committed by James Turner
parent b3b2871523
commit 7274ec886a
2 changed files with 59 additions and 1 deletions

View file

@ -148,7 +148,7 @@ var WidgetsFactoryDialog = {
m.list.createItem("Item " ~ i);
}
m.list.listen("selection-changed", func {
m.list_selection_label.setText("Selected items: " ~ (string.join(", ", map(func(item) item._text, m.list.getSelectedItems())) or "none"));
m.list_selection_label.setText("Selected items: " ~ (string.join(", ", std.map(func(item) item._text, m.list.getSelectedItems())) or "none"));
});
m.list.setSizeHint([m.list._MAX_SIZE, m.list._MAX_SIZE]);
m.list_box.addItem(m.list);

58
Nasal/std/algorithm.nas Normal file
View file

@ -0,0 +1,58 @@
# 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;
foreach (var o; obj) {
if (key) {
res &= key(o);
} else {
res &= o;
}
if (res == 0) {
break;
}
}
return res;
};
var any = func(obj, key=nil) {
var res = 0;
foreach (var o; obj) {
if (key) {
res |= key(o);
} else {
res |= o;
}
if (res == 1) {
break;
}
}
return res;
};
var map = func(f, obj) {
var res = [];
foreach (var o; obj) {
append(res, f(o));
}
return res;
};
var filter = func(f, obj) {
var res = [];
foreach (var o; obj) {
if (f) {
if (f(o)) {
append(res, o);
}
} elsif (o) {
append(res, o);
}
}
return res;
};