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 /lib.c | |
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
Diffstat (limited to 'lib.c')
-rw-r--r-- | lib.c | 28 |
1 files changed, 21 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); + } } } |