diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2020-06-03 23:27:43 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2020-06-03 23:27:43 -0700 |
commit | c988eab530b2efc3d69a55835c32e2312d3870f1 (patch) | |
tree | d57241198c6cbde0634c383bec0a813a7dd9fb7a /lib.c | |
parent | fff34360b061c93926826945d6c570da3c2fbcca (diff) | |
download | txr-c988eab530b2efc3d69a55835c32e2312d3870f1.tar.gz txr-c988eab530b2efc3d69a55835c32e2312d3870f1.tar.bz2 txr-c988eab530b2efc3d69a55835c32e2312d3870f1.zip |
Convert mapping functions to new iterators.
* eval.c (get_iter_f): Renamed to iter_from_binding_f.
(iter_begin_f, iter_more_f, iter_item_f, iter_step_f): New
global variables.
(op_each): Follow rename of get_iter_f.
(mapcarv, mappendv, lazy_mapcar_func, lazy_mapcar,
lazy_mapcarv_func, lazy_mapcarv, mapdov, prod_common):
Convert from car/cdr/null-test iteration to iter-begin.
(eval_init): gc-protect and initialize new variables.
* lib.c (mapcar_listout, mappend, mapdo): Convert to seq_iter
iteration. List argument renamed to seq.
(mapcar): List argument renamed to seq.
* lib.h: Declarations updated with renamed arguments.
Diffstat (limited to 'lib.c')
-rw-r--r-- | lib.c | 63 |
1 files changed, 24 insertions, 39 deletions
@@ -8799,35 +8799,20 @@ val copy_alist(val list) val mapcar_listout(val fun, val seq) { val self = lit("mapcar"); - seq_info_t si = seq_info(seq); - list_collect_decl (out, iter); - - switch (si.kind) { - case SEQ_NIL: - return nil; - case SEQ_VECLIKE: - { - val v = si.obj; - cnum i, len = c_fixnum(length(v), self); + seq_iter_t iter; + seq_iter_init(self, &iter, seq); + val elem; + list_collect_decl (out, ptail); - for (i = 0; i < len; i++) - iter = list_collect(iter, funcall1(fun, ref(v, num_fast(i)))); - } - break; - case SEQ_LISTLIKE: - for (seq = z(si.obj); seq; seq = cdr(seq)) - iter = list_collect(iter, funcall1(fun, car(seq))); - break; - default: - unsup_obj(self, seq); - } + while (seq_get(&iter, &elem)) + ptail = list_collect(ptail, funcall1(fun, elem)); return out; } -val mapcar(val fun, val list) +val mapcar(val fun, val seq) { - return make_like(mapcar_listout(fun, list), list); + return make_like(mapcar_listout(fun, seq), seq); } val mapcon(val fun, val list) @@ -8843,29 +8828,29 @@ val mapcon(val fun, val list) return make_like(out, list_orig); } -val mappend(val fun, val list) +val mappend(val fun, val seq) { - list_collect_decl (out, iter); - val list_orig = list; - - list = nullify(list); - - gc_hint(list); + val self = lit("mappend"); + seq_iter_t iter; + seq_iter_init(self, &iter, seq); + val elem; + list_collect_decl (out, ptail); - for (; list; list = cdr(list)) - iter = list_collect_append(iter, funcall1(fun, car(list))); + while (seq_get(&iter, &elem)) + ptail = list_collect_append(ptail, funcall1(fun, elem)); - return make_like(out, list_orig); + return make_like(out, seq); } -val mapdo(val fun, val list) +val mapdo(val fun, val seq) { - list = nullify(list); - - gc_hint(list); + val self = lit("mapdo"); + seq_iter_t iter; + seq_iter_init(self, &iter, seq); + val elem; - for (; list; list = cdr(list)) - funcall1(fun, car(list)); + while (seq_get(&iter, &elem)) + funcall1(fun, elem); return nil; } |