From 2c2fe9fc598e6677e9b16372b65c477c5ba6d03b Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 8 Jul 2020 07:36:25 -0700 Subject: list_seq: return lazy list, not eager. * lib.c (list_seq_func): New static function. (list_seq): Convert to lazy processing. The iterator is threaded through the lazy cons's car field, so we don't have to mutate the function's environment. * txr.1: Added note about list-seq producing a lazy list. --- lib.c | 26 ++++++++++++++++++-------- txr.1 | 4 ++++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/lib.c b/lib.c index a42dbbba..dc0b7161 100644 --- a/lib.c +++ b/lib.c @@ -1669,18 +1669,28 @@ val iterable(val obj) return seq_iterable(si); } +static val list_seq_func(val lcons) +{ + val iter = us_car(lcons); + val item = iter_item(iter); + val new_iter = iter_step(iter); + + us_rplaca(lcons, item); + + if (iter_more(new_iter)) + us_rplacd(lcons, make_lazy_cons_car(us_lcons_fun(lcons), new_iter)); + + return nil; +} + val list_seq(val seq) { - val self = lit("list-seq"); - seq_iter_t iter; - val elem; - seq_iter_init(self, &iter, seq); - list_collect_decl (out, ptail); + val iter = iter_begin(seq); - while (seq_get(&iter, &elem)) - ptail = list_collect(ptail, elem); + if (iter_more(iter)) + return make_lazy_cons_car(func_n1(list_seq_func), iter); - return out; + return nil; } val vec_seq(val seq) diff --git a/txr.1 b/txr.1 index 119bc1d4..bb8cf70f 100644 --- a/txr.1 +++ b/txr.1 @@ -29688,6 +29688,10 @@ and functions convert an iterable object of any type into a list, vector or string, respectively. +The list returned by +.code list-seq +is lazy. + The .code list-seq and -- cgit v1.2.3