summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2024-07-23 19:01:40 -0700
committerKaz Kylheku <kaz@kylheku.com>2024-07-23 19:01:40 -0700
commit0ccb63709201223f00365b7ee99d3ef9aa5304d2 (patch)
tree955f3fd833ec2336739207b8b8c79f8fba574433
parentff259855afa02c7bd10994f1b42739901ec028a1 (diff)
downloadtxr-0ccb63709201223f00365b7ee99d3ef9aa5304d2.tar.gz
txr-0ccb63709201223f00365b7ee99d3ef9aa5304d2.tar.bz2
txr-0ccb63709201223f00365b7ee99d3ef9aa5304d2.zip
New function: seq-like.
* eval.c (zip_fun): Renamed to seq_like. (zipv): Follow rename of zip_fun. (eval_init): Register seq-like intrinsic. * tests/seq.tl: Some tests for make-like and seq-like, revealing a difference: make-like needs to be rewritten to use seq_build. * txr.1: Documented.
-rw-r--r--eval.c5
-rw-r--r--tests/012/seq.tl14
-rw-r--r--txr.135
3 files changed, 49 insertions, 5 deletions
diff --git a/eval.c b/eval.c
index e8d57ff0..91220930 100644
--- a/eval.c
+++ b/eval.c
@@ -5864,7 +5864,7 @@ static val mapdov(val fun, varg lists)
return map_common(lit("mapdo"), fun, lists, 0, mapdo);
}
-static val zip_fun(val ziparg0, varg args)
+static val seq_like(val ziparg0, varg args)
{
seq_build_t bu;
cnum index = 0;
@@ -5898,7 +5898,7 @@ static val zipv(varg zipargs)
func = func_n0v(vectorv);
break;
default:
- func = func_f0v(ziparg0, zip_fun);
+ func = func_f0v(ziparg0, seq_like);
break;
}
@@ -7813,6 +7813,7 @@ void eval_init(void)
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("seq-like"), user_package), func_n1v(seq_like));
reg_fun(intern(lit("length"), user_package), length_f);
reg_fun(intern(lit("len"), user_package), length_f);
reg_fun(length_lt_s, func_n2(length_lt));
diff --git a/tests/012/seq.tl b/tests/012/seq.tl
index 171ad300..97549500 100644
--- a/tests/012/seq.tl
+++ b/tests/012/seq.tl
@@ -1598,3 +1598,17 @@
[(wherequal 'c) '(a b c a b c)] (2 5)
[(wherequal 3) '(3 3.0 b c a b c)] (0)
[(wherequal 3) 0..9] (3))
+
+(mtest
+ (make-like '(1 2 3) "") (1 2 3)
+ (make-like '(#\a #\b #\c) "") "abc"
+ (make-like '(#\a #\b 3) "") :error
+ (make-like '(1 2 3) #()) #(1 2 3)
+ (make-like '(1 2 3) #b'') #b'010203')
+
+(mtest
+ (seq-like "" 1 2 3) (1 2 3)
+ (seq-like "" #\a #\b #\c) "abc"
+ (seq-like "" #\a #\b 3) (#\a #\b 3)
+ (seq-like #() 1 2 3) #(1 2 3)
+ (seq-like #b'' 1 2 3) #b'010203')
diff --git a/txr.1 b/txr.1
index d643ff7f..a5fb51fa 100644
--- a/txr.1
+++ b/txr.1
@@ -34408,24 +34408,28 @@ Additional objects that are not sequences are also iterable:
numeric or character ranges, and numbers. Future revisions
of the language may specify additional iterable objects.
-.coNP Function @ make-like
+.coNP Functions @ make-like and @ seq-like
.synb
.mets (make-like < list << object )
+.mets (seq-like < object << arg *)
.syne
.desc
The
+.code make-like
+function's
.meta list
argument must be a list. If
.meta object
is a sequence type,
then
.meta list
-is converted to the same type of sequence and returned.
+is converted to the same type of sequence, if possible, and returned.
Otherwise the original
.meta list
is returned.
-Conversion is supported to string and vector type.
+Conversion is supported to string and vector type, plus
+additional types as follows.
Conversion to a structure type is possible for structures. If
.meta object
@@ -34465,6 +34469,31 @@ available, then
takes that sequence in place of
.metn object ,
+The
+.code seq-like
+function creates, if possible, a sequence of the same kind as
+.meta object
+populated by the remaining
+.meta arg
+values. If some of the
+.meta arg
+values are not suitable elements for a sequence of that type,
+then a list of those values is returned.
+
+The result of
+.code seq-like
+is consistent with what the
+.code make-like
+function would return if given a list of the
+.meta arg
+values as the
+.meta list
+argument. That is to say, the following equivalence holds:
+
+.verb
+ (make-like (list a0 a1 ...) o) <-> (seq-like o a0 a1 ...)
+.brev
+
Note: the
.code make-like
function is a helper which supports the development of