diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2012-04-14 14:11:25 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2012-04-14 14:11:25 -0700 |
commit | a82a0b4aa32dc54b5ee590e9b87e9ad635b12ecc (patch) | |
tree | 9a968f077510ce4ef2cbfa32cd1c63826a3ccd8f /lib.c | |
parent | 38ffa1f373288d9bbf29b5bb2f763a35067ab5f4 (diff) | |
download | txr-a82a0b4aa32dc54b5ee590e9b87e9ad635b12ecc.tar.gz txr-a82a0b4aa32dc54b5ee590e9b87e9ad635b12ecc.tar.bz2 txr-a82a0b4aa32dc54b5ee590e9b87e9ad635b12ecc.zip |
* eval.c (eval_init): New functions remove-if and keep-if.
* lib.c (remove_if, keep_if): New functions.
* lib.h (remove_if, keep_if): Declared.
* txr.1: Documented.
Diffstat (limited to 'lib.c')
-rw-r--r-- | lib.c | 42 |
1 files changed, 42 insertions, 0 deletions
@@ -678,6 +678,48 @@ val remqual(val obj, val list) return out; } +val remove_if(val pred, val list, val key) +{ + list_collect_decl (out, ptail); + val lastmatch = cons(nil, list); + + if (!key) + key = identity_f; + + for (; list; list = cdr(list)) { + val subj = funcall1(key, car(list)); + val satisfies = funcall1(pred, subj); + + if (satisfies) { + list_collect_nconc(ptail, ldiff(cdr(lastmatch), list)); + lastmatch = list; + } + } + list_collect_nconc(ptail, cdr(lastmatch)); + return out; +} + +val keep_if(val pred, val list, val key) +{ + list_collect_decl (out, ptail); + val lastmatch = cons(nil, list); + + if (!key) + key = identity_f; + + for (; list; list = cdr(list)) { + val subj = funcall1(key, car(list)); + val satisfies = funcall1(pred, subj); + + if (!satisfies) { + list_collect_nconc(ptail, ldiff(cdr(lastmatch), list)); + lastmatch = list; + } + } + list_collect_nconc(ptail, cdr(lastmatch)); + return out; +} + val tree_find(val obj, val tree, val testfun) { uses_or2; |