summaryrefslogtreecommitdiffstats
path: root/lib.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2023-10-05 14:56:33 -0700
committerKaz Kylheku <kaz@kylheku.com>2023-10-05 14:56:33 -0700
commit1a8ca97932bc4c923ba7d5f2ae27b2bf754884b8 (patch)
tree6a9c0cb0f36a4a422140359123abed5be407fbdd /lib.c
parent3b87081903c42cdfe0eaf58629b90a83351b7a87 (diff)
downloadtxr-1a8ca97932bc4c923ba7d5f2ae27b2bf754884b8.tar.gz
txr-1a8ca97932bc4c923ba7d5f2ae27b2bf754884b8.tar.bz2
txr-1a8ca97932bc4c923ba7d5f2ae27b2bf754884b8.zip
New: length-list-<, length-<
These are functions for testing whether a list or sequence is shorter than a given integer. This is cheaper than calculating the length of lists, which is in some cases impossible if they are infinite. A length-str-< function already exists, useful with lazy strings. length-< uses length-list-< or length-str-< as appropriate * lib.[ch] (length_list_lt, length_lt): New functions. * eval.c (eval_init): length-list-< and length-< intrinsics registered. * tests/012/seq.tl: New tests. * txr.1: Documented.
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/lib.c b/lib.c
index 0fd695a5..515d5439 100644
--- a/lib.c
+++ b/lib.c
@@ -4857,6 +4857,19 @@ val length_list(val list)
return bn_len;
}
+val length_list_lt(val list, val len)
+{
+ val self = lit("length-list-lt");
+ cnum le = c_num(len, self);
+
+ while (consp(list) && le > 0) {
+ list = cdr(list);
+ le--;
+ }
+
+ return tnil(le > 0);
+}
+
static val length_proper_list(val list)
{
cnum len = 0;
@@ -13147,6 +13160,21 @@ val length(val seq)
}
}
+val length_lt(val seq, val len)
+{
+ switch (type(seq)) {
+ case NIL:
+ return if3(plusp(len), t, nil);
+ case CONS:
+ case LCONS:
+ return length_list_lt(seq, len);
+ case LSTR:
+ return length_str_lt(seq, len);
+ default:
+ return lt(length(seq), len);
+ }
+}
+
val sub(val seq, val from, val to)
{
switch (type(seq)) {