From 2f0031c8f8efb9fb76692442f85d47f61cc9b059 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Fri, 19 Jan 2024 19:01:21 -0800 Subject: 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. --- struct.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'struct.h') diff --git a/struct.h b/struct.h index 836d60e7..7360e772 100644 --- a/struct.h +++ b/struct.h @@ -37,7 +37,7 @@ extern struct cobj_class *struct_cls; enum special_slot { equal_m, nullify_m, from_list_m, lambda_m, lambda_set_m, - length_m, car_m, cdr_m, rplaca_m, rplacd_m, + length_m, length_lt_m, car_m, cdr_m, rplaca_m, rplacd_m, iter_begin_m, iter_more_m, iter_item_m, iter_step_m, iter_reset_m, plus_m, num_special_slots -- cgit v1.2.3