diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2014-02-11 17:33:00 -0800 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2014-02-11 17:33:00 -0800 |
commit | 3c32f8de768338c0f64b1218a2f60f85d59c36ce (patch) | |
tree | 6465f8b1092d6ab46d4eae1ae9fa910f85b1643c /lib.c | |
parent | 15dda4428072bb741d6bf42fa4906b217fc2aeb1 (diff) | |
download | txr-3c32f8de768338c0f64b1218a2f60f85d59c36ce.tar.gz txr-3c32f8de768338c0f64b1218a2f60f85d59c36ce.tar.bz2 txr-3c32f8de768338c0f64b1218a2f60f85d59c36ce.zip |
* eval.c (eval_init): Register new functions posqual, posql,
posq, pos, and pos_if as intrinsics.
* lib.c (posqual, posql, posq, pos, pos_if): New functions.
* lib.h (posqual, posql, posq, pos, pos_if): Declared.
* txr.1: Documented
Diffstat (limited to 'lib.c')
-rw-r--r-- | lib.c | 66 |
1 files changed, 66 insertions, 0 deletions
@@ -4793,6 +4793,72 @@ val find_if(val pred, val list, val key) return nil; } +val posqual(val obj, val list) +{ + val pos = zero; + + for (; list; list = cdr(list), pos = plus(pos, one)) + if (equal(car(list), obj)) + return pos; + + return nil; +} + +val posql(val obj, val list) +{ + val pos = zero; + + for (; list; list = cdr(list), pos = plus(pos, one)) + if (eql(car(list), obj)) + return pos; + + return nil; +} + +val posq(val obj, val list) +{ + val pos = zero; + + for (; list; list = cdr(list), pos = plus(pos, one)) + if (eq(car(list), obj)) + return pos; + + return nil; +} + +val pos(val item, val list, val testfun, val keyfun) +{ + val pos = zero; + testfun = default_arg(testfun, equal_f); + keyfun = default_arg(keyfun, identity_f); + + for (; list; list = cdr(list), pos = plus(pos, one)) { + val elem = car(list); + val key = funcall1(keyfun, elem); + + if (funcall2(testfun, item, key)) + return pos; + } + + return nil; +} + + +val pos_if(val pred, val list, val key) +{ + val pos = zero; + key = default_arg(key, identity_f); + + for (; list; list = cdr(list), pos = plus(pos, one)) { + val item = car(list); + val subj = funcall1(key, item); + + if (funcall1(pred, subj)) + return pos; + } + + return nil; +} val set_diff(val list1, val list2, val testfun, val keyfun) { |