From 3442f8621d0a1a2e581dfbbe1aa72f8bb4ee03ca Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 15 Sep 2016 06:48:11 -0700 Subject: key function argument on remq, remql and remqual. * eval.c (weave_while): Pass third arg to remq as nil. (eval_init): Update registrations of remq, remql and remqual. * lib.c (remq, remql, remqual): Implement key function argument. * lib.h (remq, remql, remqual): Declarations updated. * sysif.c (at_exit_call): Pass third arg to remq as nil. * debug.c (debug): Pass third argument to remqual as nil. * txr.1: Documentation updated. --- lib.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'lib.c') diff --git a/lib.c b/lib.c index 0134d477..7abdb95b 100644 --- a/lib.c +++ b/lib.c @@ -1476,17 +1476,21 @@ val rmember_if(val pred, val list, val key) return found; } -val remq(val obj, val list_orig) +val remq(val obj, val list_orig, val keyfun) { list_collect_decl (out, ptail); val list = tolist(list_orig); val lastmatch = cons(nil, list); + keyfun = default_bool_arg(keyfun); gc_hint(list); for (; list; list = cdr(list)) { - if (car(list) == obj) { + val elem = car(list); + val key = keyfun ? funcall1(keyfun, elem) : elem; + + if (key == obj) { ptail = list_collect_nconc(ptail, ldiff(cdr(lastmatch), list)); lastmatch = list; } @@ -1495,16 +1499,21 @@ val remq(val obj, val list_orig) return make_like(out, list_orig); } -val remql(val obj, val list_orig) +val remql(val obj, val list_orig, val keyfun) { list_collect_decl (out, ptail); val list = tolist(list_orig); val lastmatch = cons(nil, list); + keyfun = default_bool_arg(keyfun); + gc_hint(list); for (; list; list = cdr(list)) { - if (eql(car(list), obj)) { + val elem = car(list); + val key = keyfun ? funcall1(keyfun, elem) : elem; + + if (eql(key, obj)) { ptail = list_collect_nconc(ptail, ldiff(cdr(lastmatch), list)); lastmatch = list; } @@ -1513,16 +1522,21 @@ val remql(val obj, val list_orig) return make_like(out, list_orig); } -val remqual(val obj, val list_orig) +val remqual(val obj, val list_orig, val keyfun) { list_collect_decl (out, ptail); val list = tolist(list_orig); val lastmatch = cons(nil, list); + keyfun = default_bool_arg(keyfun); + gc_hint(list); for (; list; list = cdr(list)) { - if (equal(car(list), obj)) { + val elem = car(list); + val key = keyfun ? funcall1(keyfun, elem) : elem; + + if (equal(key, obj)) { ptail = list_collect_nconc(ptail, ldiff(cdr(lastmatch), list)); lastmatch = list; } -- cgit v1.2.3