Add reduce and filter
This commit is contained in:
@@ -196,6 +196,33 @@ LISP_FUNC_2(ucl_builtin_mapcar, scope, fun, elems) {
|
||||
return result;
|
||||
}
|
||||
|
||||
LISP_FUNC_2(ucl_builtin_filter, scope, predicate, elems) {
|
||||
// TODO: Support arbitrary number of 'elems' lists
|
||||
struct ucl_object *result = ucl_nil_create();
|
||||
struct ucl_object *result_tail = result;
|
||||
FOREACH_LIST(elems, iter, elem) {
|
||||
struct ucl_object *form = ucl_tuple_create(predicate, elem);
|
||||
struct ucl_object *value = ucl_evaluate(scope, form);
|
||||
UCL_RET_IF_ERROR(value);
|
||||
if (ucl_truthy_bool(value)) {
|
||||
result_tail = ucl_list_append(result_tail, elem);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
LISP_FUNC_3(ucl_builtin_reduce, scope, fun, elems, initial_value) {
|
||||
// TODO: Support arbitrary number of 'elems' lists
|
||||
struct ucl_object *result = initial_value;
|
||||
FOREACH_LIST(elems, iter, elem) {
|
||||
struct ucl_object *form = ucl_tuple_create(fun, elem);
|
||||
ucl_list_append(form, result);
|
||||
result = ucl_evaluate(scope, form);
|
||||
UCL_RET_IF_ERROR(result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
LISP_FUNC_2(ucl_builtin_equal, scope, arg0, arg1) {
|
||||
return ucl_equal(arg0, arg1);
|
||||
}
|
||||
@@ -212,7 +239,6 @@ LISP_FUNC_2(ucl_builtin_ge, scope, arg0, arg1) {
|
||||
return ucl_predicate(arg0->integer > arg1->integer);
|
||||
}
|
||||
|
||||
|
||||
LISP_FUNC_2(ucl_builtin_lt, scope, arg0, arg1) {
|
||||
UCL_COND_OR_RET_ERROR(arg0->type == UCL_TYPE_INT, "First argument to < must be an integer");
|
||||
UCL_COND_OR_RET_ERROR(arg0->type == UCL_TYPE_INT, "Second argument to < must be an integer");
|
||||
|
||||
Reference in New Issue
Block a user