summaryrefslogtreecommitdiffstats
path: root/lib.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2024-02-27 07:17:29 -0800
committerKaz Kylheku <kaz@kylheku.com>2024-02-27 07:17:29 -0800
commitd75adfc4ea8b262ac6e0b920dc81bf0609e5c386 (patch)
tree1db2441390e02c7db196075afead6ee3d91fcc75 /lib.c
parent4177d2de918370b9cad5874952b42cb778aa00b8 (diff)
downloadtxr-d75adfc4ea8b262ac6e0b920dc81bf0609e5c386.tar.gz
txr-d75adfc4ea8b262ac6e0b920dc81bf0609e5c386.tar.bz2
txr-d75adfc4ea8b262ac6e0b920dc81bf0609e5c386.zip
separate-keys: rework using seq_build
* lib.c (separate_keys): Rewrite using seq_info and seq_build.
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c75
1 files changed, 13 insertions, 62 deletions
diff --git a/lib.c b/lib.c
index a612acff..b8aed8d6 100644
--- a/lib.c
+++ b/lib.c
@@ -3329,74 +3329,25 @@ val separate(val pred, val seq, val keyfun_in)
return cons(seq_finish(&yea), cons(seq_finish(&nay), nil));
}
-val separate_keys(val pred, val seq_in, val keyfun_in)
+val separate_keys(val pred, val seq, val keyfun_in)
{
val self = lit("separate-keys");
val keyfun = default_null_arg(keyfun_in);
+ seq_iter_t it;
+ seq_build_t yea;
+ seq_build_t nay;
+ val elem;
- switch (type(seq_in)) {
- case NIL:
- return cons(nil, cons(nil, nil));
- case CONS:
- case LCONS:
- case COBJ:
- {
- list_collect_decl (yea, yptail);
- list_collect_decl (nay, nptail);
- val list = seq_in;
-
- gc_hint(list);
-
- for (; list; list = cdr(list)) {
- val elem = car(list);
- val key = keyfun ? funcall1(keyfun, elem) : elem;
- val is_yea = if3(funcall1(pred, key), t, nil);
-
- if (is_yea)
- yptail = list_collect(yptail, key);
- else
- nptail = list_collect(nptail, key);
- }
-
- return cons(yea, cons(nay, nil));
- }
- case LIT:
- case STR:
- case LSTR:
- {
- val yea = mkustring(zero);
- val nay = mkustring(zero);
- val str = seq_in;
- cnum len = c_fixnum(length_str(str), self), i;
-
- for (i = 0; i < len; i++) {
- val elem = chr_str(str, num_fast(i));
- val key = keyfun ? funcall1(keyfun, elem) : elem;
-
- string_extend(funcall1(pred, key) ? yea : nay, key, tnil(i == len - 1));
- }
-
- return cons(yea, cons(nay, nil));
- }
- case VEC:
- {
- val yea = vector(zero, nil);
- val nay = vector(zero, nil);
- val vec = seq_in;
- cnum len = c_fixnum(length_vec(vec), self), i;
-
- for (i = 0; i < len; i++) {
- val elem = vecref(vec, num_fast(i));
- val key = keyfun ? funcall1(keyfun, elem) : elem;
-
- vec_push(funcall1(pred, key) ? yea : nay, key);
- }
+ seq_iter_init(self, &it, seq);
+ seq_build_init(&yea, seq);
+ seq_build_init(&nay, seq);
- return cons(yea, cons(nay, nil));
- }
- default:
- uw_throwf(error_s, lit("~a: ~s isn't a sequence"), self, seq_in, nao);
+ while (seq_get(&it, &elem)) {
+ val key = keyfun ? funcall1(keyfun, elem) : elem;
+ seq_add(funcall1(pred, key) ? &yea : &nay, key);
}
+
+ return cons(seq_finish(&yea), cons(seq_finish(&nay), nil));
}
static val rem_lazy_rec(val obj, val list, val env, val func);