"filter" is not a primitive

This commit is contained in:
Přemysl Eric Janouch 2017-05-23 17:54:53 +02:00
parent 120b604abd
commit 9f51b986d2
Signed by: p
GPG Key ID: B715679E3A361BE6
1 changed files with 2 additions and 31 deletions

33
ell.c
View File

@ -1046,36 +1046,6 @@ defn (fn_map) {
return check (ctx, (*result = new_list (res))); return check (ctx, (*result = new_list (res)));
} }
defn (fn_filter) {
struct item *body = args, *values;
if (!body || body->type != ITEM_LIST)
return set_error (ctx, "first argument must be a function");
if (!(values = body->next) || values->type != ITEM_LIST)
return set_error (ctx, "second argument must be a list");
struct item *res = NULL, **out = &res;
for (struct item *v = values->head; v; v = v->next) {
struct item *keep = NULL;
if (!set_single_argument (ctx, v)
|| !execute (ctx, body->head, &keep)) {
item_free_list (res);
return false;
}
bool match = truthy (keep);
item_free_list (keep);
if (!match)
continue;
struct item *copy;
if (!check (ctx, (copy = new_clone (v)))) {
item_free_list (res);
return false;
}
out = &(*out = copy)->next;
}
return check (ctx, (*result = new_list (res)));
}
defn (fn_print) { defn (fn_print) {
(void) result; (void) result;
for (; args; args = args->next) { for (; args; args = args->next) {
@ -1247,6 +1217,8 @@ init_runtime_library (struct context *ctx) {
const char *definition; ///< The defining script const char *definition; ///< The defining script
} functions[] = { } functions[] = {
{ "unless", "arg _cond _body; if (not (@_cond)) @_body" }, { "unless", "arg _cond _body; if (not (@_cond)) @_body" },
{ "filter", "arg _body _list; map {"
"arg _item; if (@_body @_item) { @_item } } @_list" },
// TODO: we should be able to apply them to all arguments // TODO: we should be able to apply them to all arguments
{ "ne?", "arg _ne1 _ne2; not (eq? @_ne1 @_ne2)" }, { "ne?", "arg _ne1 _ne2; not (eq? @_ne1 @_ne2)" },
{ "ge?", "arg _ge1 _ge2; not (lt? @_ge1 @_ge2)" }, { "ge?", "arg _ge1 _ge2; not (lt? @_ge1 @_ge2)" },
@ -1285,7 +1257,6 @@ init_runtime_library (struct context *ctx) {
&& native_register (ctx, "for", fn_for) && native_register (ctx, "for", fn_for)
&& native_register (ctx, "break", fn_break) && native_register (ctx, "break", fn_break)
&& native_register (ctx, "map", fn_map) && native_register (ctx, "map", fn_map)
&& native_register (ctx, "filter", fn_filter)
&& native_register (ctx, "print", fn_print) && native_register (ctx, "print", fn_print)
&& native_register (ctx, "..", fn_concatenate) && native_register (ctx, "..", fn_concatenate)
&& native_register (ctx, "system", fn_system) && native_register (ctx, "system", fn_system)