summaryrefslogtreecommitdiffstats
path: root/lib.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2024-05-26 21:25:20 -0700
committerKaz Kylheku <kaz@kylheku.com>2024-05-26 21:25:20 -0700
commit89eb0e18c6af1dc2150e34af19e81b1ad3b52f81 (patch)
tree1de1a1a957087d318582c2126c0d07c3355f64f2 /lib.c
parent5b3398c05bfdb7dcb448d66814256b509e45b7e5 (diff)
downloadtxr-89eb0e18c6af1dc2150e34af19e81b1ad3b52f81.tar.gz
txr-89eb0e18c6af1dc2150e34af19e81b1ad3b52f81.tar.bz2
txr-89eb0e18c6af1dc2150e34af19e81b1ad3b52f81.zip
interpose: use seq_iter and seq_build.
* lib.c (interpose): non-list cases consolidated into one, which uses generic iteration and building. * tests/012/seq.tl: New tests
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c28
1 files changed, 21 insertions, 7 deletions
diff --git a/lib.c b/lib.c
index 1d06d645..7a0d2522 100644
--- a/lib.c
+++ b/lib.c
@@ -11151,6 +11151,8 @@ static val lazy_interpose(val sep, val list)
val interpose(val sep, val seq)
{
+ val self = lit("interpose");
+
switch (type(seq)) {
case NIL:
return nil;
@@ -11171,14 +11173,26 @@ val interpose(val sep, val seq)
}
case LCONS:
return lazy_interpose(sep, seq);
- case LIT:
- case STR:
- case LSTR:
- return cat_str(interpose(sep, tolist(seq)), nil);
- case VEC:
- return vec_list(interpose(sep, tolist(seq)));
default:
- type_mismatch(lit("interpose: ~s is not a sequence"), seq, nao);
+ {
+ seq_build_t bu;
+ seq_iter_t it;
+ val elem;
+
+ seq_build_init(self, &bu, seq);
+ seq_iter_init(self, &it, seq);
+
+ if (seq_get(&it, &elem)) {
+ seq_add(&bu, elem);
+
+ while (seq_get(&it, &elem)) {
+ seq_add(&bu, sep);
+ seq_add(&bu, elem);
+ }
+ }
+
+ return seq_finish(&bu);
+ }
}
}