From 7aef72ae4fd0fcbe18acbbd876fed0aaf6bf87ee Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 13 Mar 2019 06:48:23 -0700 Subject: lazy conses: support state in car and cdr. Here we allow application code to take advantage of a trick already used internally. When a lazy cons cell is created, we can temporarily put state information into its car and cdr fields. When these fields are accessed normally, by the car and cdr function, the lazy cons' update function will be invoked, which will populate these fields. If we have a way for that function to retrieve the existing values of those fields, then the function can treat those fields as state information: it can retrieve the values into temporary local variables, overwrite the original values, and then propagate the state information into the car and cdr into the next lazy cons cell being added. Thus lazy list generation that needs two cells of state or less does not require the allocation of a context object. * eval.c (eval_init): make-lazy-cons becomes a three-argument function with two optional parameters. New functions lcons-car and lcons-cdr are registered. * lib.c (make_lazy_cons_pub): New function, wrapping make_lazy_cons_car_cdr with argument defaulting. (lcons_car, lcons_cdr): New functions. * lib.h (make_lazy_cons_pu, lcons_car, lcons_cdr): Declared. * txr.1: Updated doc of make-lazy-cons regarding new arguments. Documented lcons-car and lcons-cdr. --- lib.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'lib.c') diff --git a/lib.c b/lib.c index 1ca81fc8..06cd37c1 100644 --- a/lib.c +++ b/lib.c @@ -3022,6 +3022,23 @@ val make_lazy_cons_car_cdr(val func, val car, val cdr) return obj; } +val make_lazy_cons_pub(val func, val car, val cdr) +{ + return make_lazy_cons_car_cdr(func, default_null_arg(car), default_null_arg(cdr)); +} + +val lcons_car(val lcons) +{ + type_check(lit("lcons-car"), lcons, LCONS); + return lcons->lc.car; +} + +val lcons_cdr(val lcons) +{ + type_check(lit("lcons-cdr"), lcons, LCONS); + return lcons->lc.cdr; +} + void rcyc_cons(val cons) { cons->c.cdr = recycled_conses; -- cgit v1.2.3