summaryrefslogtreecommitdiffstats
path: root/hash.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2023-07-10 23:10:17 -0700
committerKaz Kylheku <kaz@kylheku.com>2023-07-10 23:10:17 -0700
commita011544efd7e826e1bd07cce0cdf52ab7be245f2 (patch)
treee139bfc0dfe1ee47eff1af57e138b86e86000abf /hash.c
parentcfe3269e978e06cf0fb3f27f248bfde0a77d0038 (diff)
downloadtxr-a011544efd7e826e1bd07cce0cdf52ab7be245f2.tar.gz
txr-a011544efd7e826e1bd07cce0cdf52ab7be245f2.tar.bz2
txr-a011544efd7e826e1bd07cce0cdf52ab7be245f2.zip
group-by: use sequence iteration.
* hash.c (group_by): Replace obsolete list/vector switching with seq_iter_t iteration. The group-by function is limited otherwise. * txr.1: Update group-by documentation not to assert that the sequence argument is required to be a list or vector. Examples for group-by and group-map now use a 0..11 range instead of (range 0 10).
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c17
1 files changed, 5 insertions, 12 deletions
diff --git a/hash.c b/hash.c
index 8932a856..cfd4826c 100644
--- a/hash.c
+++ b/hash.c
@@ -1761,20 +1761,13 @@ val group_by(val func, val seq, struct args *hashv_args)
{
val self = lit("group-by");
val hash = hashv(hashv_args);
+ seq_iter_t iter;
+ val elem;
- if (vectorp(seq)) {
- cnum i, len;
+ seq_iter_init(self, &iter, seq);
- for (i = 0, len = c_fixnum(length(seq), self); i < len; i++) {
- val v = vecref(seq, num_fast(i));
- pushhash(hash, funcall1(func, v), v);
- }
- } else {
- for (; seq; seq = cdr(seq)) {
- val v = car(seq);
- pushhash(hash, funcall1(func, v), v);
- }
- }
+ while (seq_get(&iter, &elem))
+ pushhash(hash, funcall1(func, elem), elem);
{
struct hash_iter hi;