diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2016-09-15 06:48:11 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2016-09-15 06:48:11 -0700 |
commit | 3442f8621d0a1a2e581dfbbe1aa72f8bb4ee03ca (patch) | |
tree | 19c02de1d17eb709ec77b8458c7c679fc705b562 /lib.c | |
parent | 6fe8c83faa4e97aee190049b175e1b6b2e25aa97 (diff) | |
download | txr-3442f8621d0a1a2e581dfbbe1aa72f8bb4ee03ca.tar.gz txr-3442f8621d0a1a2e581dfbbe1aa72f8bb4ee03ca.tar.bz2 txr-3442f8621d0a1a2e581dfbbe1aa72f8bb4ee03ca.zip |
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.
Diffstat (limited to 'lib.c')
-rw-r--r-- | lib.c | 26 |
1 files changed, 20 insertions, 6 deletions
@@ -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; } |