refactor map and for-each

This commit is contained in:
Yuichi Nishiwaki 2014-09-27 20:15:47 +09:00
parent 552ee7444f
commit c808b34a67
1 changed files with 14 additions and 12 deletions

26
pair.c
View File

@ -643,23 +643,24 @@ pic_pair_map(pic_state *pic)
struct pic_proc *proc; struct pic_proc *proc;
int argc, i; int argc, i;
pic_value *args; pic_value *args;
pic_value cars, ret; pic_value arg, ret;
pic_get_args(pic, "l*", &proc, &argc, &args); pic_get_args(pic, "l*", &proc, &argc, &args);
ret = pic_nil_value(); ret = pic_nil_value();
do { do {
cars = pic_nil_value(); arg = pic_nil_value();
for (i = argc - 1; i >= 0; --i) { for (i = 0; i < argc; ++i) {
if (! pic_pair_p(args[i])) { if (! pic_pair_p(args[i])) {
break; 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]); args[i] = pic_cdr(pic, args[i]);
} }
if (i >= 0) if (i != argc) {
break; 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); } while (1);
return pic_reverse(pic, ret); return pic_reverse(pic, ret);
@ -671,22 +672,23 @@ pic_pair_for_each(pic_state *pic)
struct pic_proc *proc; struct pic_proc *proc;
int argc, i; int argc, i;
pic_value *args; pic_value *args;
pic_value cars; pic_value arg;
pic_get_args(pic, "l*", &proc, &argc, &args); pic_get_args(pic, "l*", &proc, &argc, &args);
do { do {
cars = pic_nil_value(); arg = pic_nil_value();
for (i = argc - 1; i >= 0; --i) { for (i = 0; i < argc; ++i) {
if (! pic_pair_p(args[i])) { if (! pic_pair_p(args[i])) {
break; 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]); args[i] = pic_cdr(pic, args[i]);
} }
if (i >= 0) if (i != argc) {
break; break;
pic_apply(pic, proc, cars); }
pic_apply(pic, proc, pic_reverse(pic, arg));
} while (1); } while (1);
return pic_none_value(); return pic_none_value();