From c808b34a677815886f784c4c89d4fbf821d9f397 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 27 Sep 2014 20:15:47 +0900 Subject: [PATCH] refactor map and for-each --- pair.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/pair.c b/pair.c index 184182cc..cbb4de70 100644 --- a/pair.c +++ b/pair.c @@ -643,23 +643,24 @@ pic_pair_map(pic_state *pic) struct pic_proc *proc; int argc, i; pic_value *args; - pic_value cars, ret; + pic_value arg, ret; pic_get_args(pic, "l*", &proc, &argc, &args); ret = pic_nil_value(); do { - cars = pic_nil_value(); - for (i = argc - 1; i >= 0; --i) { + arg = pic_nil_value(); + for (i = 0; i < argc; ++i) { if (! pic_pair_p(args[i])) { break; } - cars = pic_cons(pic, pic_car(pic, args[i]), cars); + pic_push(pic, pic_car(pic, args[i]), arg); args[i] = pic_cdr(pic, args[i]); } - if (i >= 0) + if (i != argc) { break; - ret = pic_cons(pic, pic_apply(pic, proc, cars), ret); + } + pic_push(pic, pic_apply(pic, proc, pic_reverse(pic, arg)), ret); } while (1); return pic_reverse(pic, ret); @@ -671,22 +672,23 @@ pic_pair_for_each(pic_state *pic) struct pic_proc *proc; int argc, i; pic_value *args; - pic_value cars; + pic_value arg; pic_get_args(pic, "l*", &proc, &argc, &args); do { - cars = pic_nil_value(); - for (i = argc - 1; i >= 0; --i) { + arg = pic_nil_value(); + for (i = 0; i < argc; ++i) { if (! pic_pair_p(args[i])) { break; } - cars = pic_cons(pic, pic_car(pic, args[i]), cars); + pic_push(pic, pic_car(pic, args[i]), arg); args[i] = pic_cdr(pic, args[i]); } - if (i >= 0) + if (i != argc) { break; - pic_apply(pic, proc, cars); + } + pic_apply(pic, proc, pic_reverse(pic, arg)); } while (1); return pic_none_value();