summaryrefslogtreecommitdiffstats
path: root/lib.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2023-07-10 23:51:52 -0700
committerKaz Kylheku <kaz@kylheku.com>2023-07-10 23:51:52 -0700
commit1247bc2eaa8db74bb4d60456aea1c344d41dad9d (patch)
tree4a7bbe0d036e9491caa3620ee6fe5177470c8706 /lib.c
parent93707b7aa65a7050950bc9d0729f319e59f13dbf (diff)
downloadtxr-1247bc2eaa8db74bb4d60456aea1c344d41dad9d.tar.gz
txr-1247bc2eaa8db74bb4d60456aea1c344d41dad9d.tar.bz2
txr-1247bc2eaa8db74bb4d60456aea1c344d41dad9d.zip
unique: use sequence iteration
* lib.c (unique): Use seq_iter_t rather than dividing into list and vector cases.
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c28
1 files changed, 8 insertions, 20 deletions
diff --git a/lib.c b/lib.c
index ec62119c..8ba7cd3c 100644
--- a/lib.c
+++ b/lib.c
@@ -11339,31 +11339,19 @@ val unique(val seq, val keyfun, struct args *hashv_args)
val self = lit("unique");
val hash = hashv(hashv_args);
val kf = default_arg(keyfun, identity_f);
-
+ seq_iter_t iter;
+ val elem;
list_collect_decl (out, ptail);
- if (vectorp(seq) || stringp(seq)) {
- cnum i, len;
-
- for (i = 0, len = c_fixnum(length(seq), self); i < len; i++) {
- val new_p;
- val v = ref(seq, num_fast(i));
-
- (void) gethash_c(self, hash, funcall1(kf, v), mkcloc(new_p));
+ seq_iter_init(self, &iter, seq);
- if (new_p)
- ptail = list_collect(ptail, v);
- }
- } else {
- for (; seq; seq = cdr(seq)) {
- val new_p;
- val v = car(seq);
+ while (seq_get(&iter, &elem)) {
+ val new_p;
- (void) gethash_c(self, hash, funcall1(kf, v), mkcloc(new_p));
+ (void) gethash_c(self, hash, funcall1(kf, elem), mkcloc(new_p));
- if (new_p)
- ptail = list_collect(ptail, v);
- }
+ if (new_p)
+ ptail = list_collect(ptail, elem);
}
return make_like(out, seq);