From ee9fb41197c8a948144b9f384da273688bdd538c Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Tue, 27 Feb 2024 20:13:01 -0800 Subject: mapcar, mappend: switch to seq_build. * lib.c (mapcar): Implement with seq_iter and seq_build, rather than relying on mapcar_listout and make_like. (mappend): Replace list_collect_decl and make_like with seq_build. * eval.c (map_common): Replace list_collect_decl and make_like with seq_build. The collect_fn is now a pointer to either seq_add or seq_pend rather than list_collect or list_collect_append. (mapcarv, mappendv): Pass seq_add and seq_pend to map_common, rather than list_collect and list_collect_append. --- eval.c | 15 +++++++++------ lib.c | 20 ++++++++++++++++---- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/eval.c b/eval.c index 4de90576..675c1e04 100644 --- a/eval.c +++ b/eval.c @@ -5766,7 +5766,7 @@ static val abscond_star(val name, val retval) } static val map_common(val self, val fun, varg lists, - loc (*collect_fn)(loc ptail, val obj), + void (*collect_fn)(seq_build_t *, val), val (*map_fn)(val fun, val seq)) { if (!args_more(lists, 0)) { @@ -5778,8 +5778,11 @@ static val map_common(val self, val fun, varg lists, val arg0 = args_at(lists, 0); seq_iter_t *iter_array = coerce(seq_iter_t *, alloca(argc * sizeof *iter_array)); + seq_build_t out = { 0 }; args_decl(args_fun, max(argc, ARGS_MIN)); - list_collect_decl (out, otail); + + if (collect_fn != 0) + seq_build_init(self, &out, arg0); for (i = 0, idx = 0; i < argc; i++) { @@ -5795,7 +5798,7 @@ static val map_common(val self, val fun, varg lists, seq_iter_t *iter = &iter_array[i]; if (!seq_get(iter, &elem)) - return collect_fn != 0 ? make_like(out, arg0) : nil; + return collect_fn != 0 ? seq_finish(&out) : nil; args_fun->arg[i] = elem; } @@ -5806,14 +5809,14 @@ static val map_common(val self, val fun, varg lists, fun_ret = generic_funcall(fun, args_fun); if (collect_fn != 0) - otail = collect_fn(otail, fun_ret); + collect_fn(&out, fun_ret); } } } val mapcarv(val fun, varg lists) { - return map_common(lit("mapcar"), fun, lists, list_collect, mapcar); + return map_common(lit("mapcar"), fun, lists, seq_add, mapcar); } val mapcarl(val fun, val list_of_lists) @@ -5824,7 +5827,7 @@ val mapcarl(val fun, val list_of_lists) static val mappendv(val fun, varg lists) { - return map_common(lit("mappend"), fun, lists, list_collect_append, mappend); + return map_common(lit("mappend"), fun, lists, seq_pend, mappend); } static val mapdov(val fun, varg lists) diff --git a/lib.c b/lib.c index 26eb8d45..9acb9abe 100644 --- a/lib.c +++ b/lib.c @@ -10889,7 +10889,18 @@ val mapcar_listout(val fun, val seq) val mapcar(val fun, val seq) { - return make_like(mapcar_listout(fun, seq), seq); + val self = lit("mapcar"); + seq_iter_t iter; + seq_build_t build; + val elem; + + seq_iter_init(self, &iter, seq); + seq_build_init(self, &build, seq); + + while (seq_get(&iter, &elem)) + seq_add(&build, funcall1(fun, elem)); + + return seq_finish(&build); } val mapcon(val fun, val list) @@ -10909,15 +10920,16 @@ val mappend(val fun, val seq) { val self = lit("mappend"); seq_iter_t iter; + seq_build_t build; val elem; - list_collect_decl (out, ptail); seq_iter_init(self, &iter, seq); + seq_build_init(self, &build, seq); while (seq_get(&iter, &elem)) - ptail = list_collect_append(ptail, funcall1(fun, elem)); + seq_pend(&build, funcall1(fun, elem)); - return make_like(out, seq); + return seq_finish(&build); } val mapdo(val fun, val seq) -- cgit v1.2.3