From 3dbaecd359c584e1bcf650cdf53fb35253599e15 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 26 Jun 2014 06:56:10 -0700 Subject: * eval.c (mapcarv): Use mapcar_listout, so list_of_lists can be a non-list sequence. (eval_init): Register transpose and zip as intrinsics. * lib.c (curry_12_1_v): New static function. (transpose, mapcar_listout): New functions. (mapcar): Redefined in terms of mapcar_listout. * lib.h (transpose, mapcar_listout): Declared. * txr.1: Documented transpose and zip. --- lib.c | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) (limited to 'lib.c') diff --git a/lib.c b/lib.c index 61390692..422396f0 100644 --- a/lib.c +++ b/lib.c @@ -3894,6 +3894,11 @@ val curry_12_1(val fun2, val arg2) return func_f1(cons(fun2, arg2), do_curry_12_1); } +static val curry_12_1_v(val fun2, val arg2) +{ + return func_f0v(cons(fun2, arg2), do_curry_12_1); +} + static val do_curry_123_3(val fcons, val arg3) { return funcall3(car(fcons), car(cdr(fcons)), cdr(cdr(fcons)), arg3); @@ -3944,6 +3949,26 @@ val curry_1234_34(val fun4, val arg1, val arg2) return func_f2(cons(fun4, cons(arg1, arg2)), do_curry_1234_34); } +val transpose(val list) +{ + val func = list_f; + + switch (type(car(list))) { + case STR: + case LSTR: + case LIT: + func = curry_12_1_v(func_n2(cat_str), nil); + break; + case VEC: + func = func_n0v(vector_list); + break; + default: + break; + } + + return make_like(mapcarv(func, list), list); +} + static val do_chain(val fun1_list, val args) { val arg = nil; @@ -4821,17 +4846,21 @@ val copy_alist(val list) return mapcar(func_n1(copy_cons), list); } -val mapcar(val fun, val list) +val mapcar_listout(val fun, val list) { list_collect_decl (out, iter); - val list_orig = list; list = nullify(list); for (; list; list = cdr(list)) iter = list_collect(iter, funcall1(fun, car(list))); - return make_like(out, list_orig); + return out; +} + +val mapcar(val fun, val list) +{ + return make_like(mapcar_listout(fun, list), list); } val mapcon(val fun, val list) -- cgit v1.2.3