diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2024-05-26 21:25:20 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2024-05-26 21:25:20 -0700 |
commit | 89eb0e18c6af1dc2150e34af19e81b1ad3b52f81 (patch) | |
tree | 1de1a1a957087d318582c2126c0d07c3355f64f2 | |
parent | 5b3398c05bfdb7dcb448d66814256b509e45b7e5 (diff) | |
download | txr-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
-rw-r--r-- | lib.c | 28 | ||||
-rw-r--r-- | tests/012/seq.tl | 11 |
2 files changed, 32 insertions, 7 deletions
@@ -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); + } } } diff --git a/tests/012/seq.tl b/tests/012/seq.tl index 262c7739..c3000b5e 100644 --- a/tests/012/seq.tl +++ b/tests/012/seq.tl @@ -883,3 +883,14 @@ (mtest (ref 1..6 0.0) (1.0 2.0 3.0 4.0 5.0)) + +(mtest + (interpose 1 '()) () + (interpose 1 '(a)) (a) + (interpose 1 '(a b)) (a 1 b) + (interpose 1 '(a b c)) (a 1 b 1 c)) + +(mtest + (interpose 1 "") "" + (interpose 1 "a") "a" + (interpose 1 "ab") (#\a 1 #\b)) |