diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2023-07-10 23:10:17 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2023-07-10 23:10:17 -0700 |
commit | a011544efd7e826e1bd07cce0cdf52ab7be245f2 (patch) | |
tree | e139bfc0dfe1ee47eff1af57e138b86e86000abf | |
parent | cfe3269e978e06cf0fb3f27f248bfde0a77d0038 (diff) | |
download | txr-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).
-rw-r--r-- | hash.c | 17 | ||||
-rw-r--r-- | txr.1 | 9 |
2 files changed, 9 insertions, 17 deletions
@@ -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; @@ -56036,9 +56036,8 @@ is also returned. The .code group-by function produces a hash table from -.metn sequence , -which is a -list or vector. Entries of the hash table are not elements of +.metn sequence . +Entries of the hash table are not elements of .metn sequence , but lists of elements of .metn sequence . @@ -56088,7 +56087,7 @@ Group the integers from 0 to 10 into three buckets keyed on 0, 1 and 2 according to the modulo 3 congruence: .verb - (group-by (op mod @1 3) (range 0 10)) + (group-by (op mod @1 3) 0..11) -> #H(() (0 (0 3 6 9)) (1 (1 4 7 10)) (2 (2 5 8))) .brev @@ -56096,7 +56095,7 @@ Same as above, but associate the keys with the sums of the buckets: .verb - [group-map (op mod @1 3) sum (range 0 10)] + [group-map (op mod @1 3) sum 0..11] -> #H(() (0 18) (1 22) (2 15)) .brev |