diff --git a/builtin.c b/builtin.c index 2c5787da..b572dfaf 100644 --- a/builtin.c +++ b/builtin.c @@ -241,6 +241,56 @@ static jv f_group_by_impl(jv input, jv keys) { } } +static jv minmax_by(jv values, jv keys, int is_min) { + if (jv_get_kind(values) != JV_KIND_ARRAY) + return type_error2(values, keys, "cannot be iterated over"); + if (jv_get_kind(keys) != JV_KIND_ARRAY) + return type_error2(values, keys, "cannot be iterated over"); + if (jv_array_length(jv_copy(values)) != jv_array_length(jv_copy(keys))) + return type_error2(values, keys, "have wrong length"); + + if (jv_array_length(jv_copy(values)) == 0) { + jv_free(values); + jv_free(keys); + return jv_null(); + } + jv ret = jv_array_get(jv_copy(values), 0); + jv retkey = jv_array_get(jv_copy(keys), 0); + for (int i=1; i