summaryrefslogtreecommitdiffstats
path: root/lib.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2014-06-18 06:50:23 -0700
committerKaz Kylheku <kaz@kylheku.com>2014-06-18 06:50:23 -0700
commit990b739d9f20664aa62ec23fb9458953a5fb2cd4 (patch)
treefea47c123e464c74b121bf085c217cbdf26ec4b8 /lib.c
parenta3380a6ce354fc956635837d3fbca39b730d73aa (diff)
downloadtxr-990b739d9f20664aa62ec23fb9458953a5fb2cd4.tar.gz
txr-990b739d9f20664aa62ec23fb9458953a5fb2cd4.tar.bz2
txr-990b739d9f20664aa62ec23fb9458953a5fb2cd4.zip
* eval.c (eval_init): Register member and member_if as intrinsics.
* lib.c (member, member_if): New functions. * lib.h (member, member_if): Declared. * txr.1: Documented. * txr.vim: Regenerated.
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/lib.c b/lib.c
index 96ede9f9..2507d8f6 100644
--- a/lib.c
+++ b/lib.c
@@ -914,6 +914,41 @@ val memqual(val obj, val list)
return make_like(list, list_orig);
}
+val member(val item, val list, val testfun, val keyfun)
+{
+ testfun = default_arg(testfun, equal_f);
+ keyfun = default_arg(keyfun, identity_f);
+
+ list = nullify(list);
+
+ for (; list; list = cdr(list)) {
+ val elem = car(list);
+ val key = funcall1(keyfun, elem);
+
+ if (funcall2(testfun, item, key))
+ return list;
+ }
+
+ return nil;
+}
+
+val member_if(val pred, val list, val key)
+{
+ key = default_arg(key, identity_f);
+ list = nullify(list);
+
+ for (; list; list = cdr(list)) {
+ val item = car(list);
+ val subj = funcall1(key, item);
+
+ if (funcall1(pred, subj))
+ return list;
+ }
+
+ return nil;
+}
+
+
val remq(val obj, val list)
{
list_collect_decl (out, ptail);