From 867162ac77d5e35ded33a44578d6ef0eeb16cd49 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sun, 28 Jun 2020 08:48:20 -0700 Subject: New functions: list-seq, ved-seq and str-seq. These functions convert any iterable to a list, vector or string. * eval.c (eval_init): Registered list-seq, vec-seq and str-seq intrinsics. * lib.c (list_seq, vec_seq, str_seq): New functions. * lib.h (list_seq, vec_seq, str_seq): Declared. * txr.1: Documented. --- eval.c | 3 +++ lib.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ lib.h | 3 +++ txr.1 | 29 +++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+) diff --git a/eval.c b/eval.c index 405851f1..cd282da0 100644 --- a/eval.c +++ b/eval.c @@ -6926,6 +6926,9 @@ void eval_init(void) reg_fun(intern(lit("uni"), user_package), func_n4o(uni, 2)); reg_fun(intern(lit("seqp"), user_package), func_n1(seqp)); + reg_fun(intern(lit("list-seq"), user_package), func_n1(list_seq)); + reg_fun(intern(lit("vec-seq"), user_package), func_n1(vec_seq)); + reg_fun(intern(lit("str-seq"), user_package), func_n1(str_seq)); reg_fun(intern(lit("length"), user_package), length_f); reg_fun(intern(lit("len"), user_package), length_f); reg_fun(intern(lit("empty"), user_package), func_n1(empty)); diff --git a/lib.c b/lib.c index c2180abb..040294b5 100644 --- a/lib.c +++ b/lib.c @@ -1486,6 +1486,52 @@ val seqp(val obj) return tnil(si.kind != SEQ_NOTSEQ); } +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); + + while (seq_get(&iter, &elem)) + ptail = list_collect(ptail, elem); + + return out; +} + +val vec_seq(val seq) +{ + val self = lit("vec-seq"); + seq_iter_t iter; + val elem; + val vec = vector(zero, nil); + seq_iter_init(self, &iter, seq); + + while (seq_get(&iter, &elem)) + vec_push(vec, elem); + + return vec; +} + +val str_seq(val seq) +{ + val self = lit("str-seq"); + seq_iter_t iter; + val elem; + val str = mkustring(zero); + seq_iter_init(self, &iter, seq); + + while (seq_get(&iter, &elem)) { + if (chrp(elem) || stringp(elem)) + string_extend(str, elem); + else + unsup_obj(self, elem); + } + + return str; +} + loc list_collect(loc ptail, val obj) { val items = cons(obj, nil); diff --git a/lib.h b/lib.h index dff5f178..3e97d45d 100644 --- a/lib.h +++ b/lib.h @@ -620,6 +620,9 @@ val tolist(val seq); val nullify(val obj); val empty(val seq); val seqp(val obj); +val list_seq(val seq); +val vec_seq(val seq); +val str_seq(val seq); val nreverse(val in); val reverse(val in); val us_nreverse(val in); diff --git a/txr.1 b/txr.1 index 84b1dab7..c81b4523 100644 --- a/txr.1 +++ b/txr.1 @@ -29227,6 +29227,35 @@ then convert the resulting list to the same type as an input value by using .codn make-like . +.coNP Functions @, list-seq @ vec-seq and @ str-seq +.synb +.mets (list-seq << iterable ) +.mets (vec-seq << iterable ) +.mets (str-seq << iterable ) +.syne +.desc +The +.codn list-seq , +.code vec-seq +and +.code str-seq +functions convert an iterable object of any type into a list, vector +or string, respectively. + +The +.code list-seq +and +.code vec-seq +iterate the items of +.meta iterable +and accumulate these items into a new list or vector. + +The +.code str-seq +similarly iterates the items of +.metn iterable , +requiring them to be a mixture of characters and strings. + .coNP Functions @ length and @ len .synb .mets (length << iterable ) -- cgit v1.2.3