summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2024-01-19 19:01:21 -0800
committerKaz Kylheku <kaz@kylheku.com>2024-01-19 19:01:21 -0800
commit2f0031c8f8efb9fb76692442f85d47f61cc9b059 (patch)
treec96da51a2d0873ca9407d129b4a54f47b1498b39 /tests
parentfbf3ae2f5c6c89c3d48ffe40760b9f31d7dfbadd (diff)
downloadtxr-2f0031c8f8efb9fb76692442f85d47f61cc9b059.tar.gz
txr-2f0031c8f8efb9fb76692442f85d47f61cc9b059.tar.bz2
txr-2f0031c8f8efb9fb76692442f85d47f61cc9b059.zip
We need a length-< special method.
Structure objects can be used to implement lazy structures such as sequences. It is undesirable to take the length of a lazy sequence because it forces all of its elements to exist. Moreover, if the sequence is infinite, it is impossible. There are situations in which it is only necessary to know whether the length is less than a certain bound, and for that we have the length-< function. That works on infinite sequence such as lazy lists, requiring them to be forced only so far as to determine the truth value of the test. We need objects that implement lazy sequences to work with this function. * struct.h (enum special_slot): New member length_lt_m. * lib.h (length_lt_s): Symbol variable declared. * struct.c (special_sym): New entry in this table, associating the length_lt_m enum with the length_lt_s symbol variable. * lib.c (length_lt_s): Symbol variable defined. (length_lt): Handle COBJ objects that are structures. we test whether they have a length-< method, or else length method. If they don't have either, we throw. We don't fall back on the default case for objects that don't have a length-< method, because the diagnostic won't be good if they don't have a length method either; the programmer will be informed that the length function couldn't find a length method, without mentioning that it was actually length-< that is being used. * eval.c (eval_init): Register length-< using the length_lt_s symbol variable rather than using intern. * txr.1: Documented. * tests/012/oop-seq.tl: New tests.
Diffstat (limited to 'tests')
-rw-r--r--tests/012/oop-seq.tl17
1 files changed, 17 insertions, 0 deletions
diff --git a/tests/012/oop-seq.tl b/tests/012/oop-seq.tl
index e91564fc..17463e96 100644
--- a/tests/012/oop-seq.tl
+++ b/tests/012/oop-seq.tl
@@ -55,6 +55,23 @@
(test (list-seq (new counter-fast init 0 step 1 limit 0))
nil)
+(defstruct integers ()
+ item to next
+ (:method length-< (me len)
+ (cond
+ ((<= len 1) nil)
+ (me.next me.next.(length-< (pred len)))
+ (t)))
+ (:postinit (me)
+ (if (< me.item me.to)
+ (set me.next (lnew integers to me.to item (succ me.item))))))
+
+(let ((ints (new integers item 1 to 10)))
+ (mtest
+ (length-< ints 11) t)
+ (length-< ints 10) nil
+ (length-< ints 9) nil)
+
;; The following reproduced a segfault when the change was made to allow del to
;; work with structs that have lambda and lambda-set.