diff options
-rw-r--r-- | hash.c | 17 | ||||
-rw-r--r-- | hash.h | 1 | ||||
-rw-r--r-- | txr.1 | 30 |
3 files changed, 48 insertions, 0 deletions
@@ -1249,6 +1249,22 @@ val hash_list(val keys, struct args *hashv_args) return hash; } +val hash_zip(val keys, val vals, struct args *hashv_args) +{ + val self = lit("hash-zip"); + seq_iter_t key_iter, val_iter; + val k, v; + val hash = hashv(hashv_args); + + seq_iter_init(self, &key_iter, keys); + seq_iter_init(self, &val_iter, vals); + + while (seq_get(&key_iter, &k) && seq_get(&val_iter, &v)) + sethash(hash, k, v); + + return hash; +} + val group_by(val func, val seq, struct args *hashv_args) { val self = lit("group-by"); @@ -1642,6 +1658,7 @@ void hash_init(void) reg_fun(intern(lit("hash-from-pairs"), user_package), func_n1v(hash_from_pairs_v)); reg_fun(intern(lit("hash-from-alist"), user_package), func_n1v(hash_from_alist_v)); reg_fun(intern(lit("hash-list"), user_package), func_n1v(hash_list)); + reg_fun(intern(lit("hash-zip"), user_package), func_n2v(hash_zip)); reg_fun(intern(lit("gethash"), user_package), func_n3o(gethash_n, 2)); reg_fun(intern(lit("inhash"), user_package), func_n3o(inhash, 2)); reg_fun(intern(lit("sethash"), user_package), func_n3(sethash)); @@ -58,6 +58,7 @@ val hash_construct(val hashl_args, val pairs); val hash_from_pairs_v(val pairs, struct args *hashv_args); val hash_from_alist_v(val alist, struct args *hashv_args); val hash_list(val keys, struct args *hashv_args); +val hash_zip(val keys, val vals, struct args *hashv_args); val group_by(val func, val seq, struct args *hashv_args); val group_reduce(val hash, val by_fun, val reduce_fun, val seq, val initval, val filter_fun); @@ -43691,6 +43691,36 @@ and returned. The value associated with each key is that key itself. +.coNP Function @ hash-zip +.synb +.mets (hash-zip < key-seq < value-seq << hash-arg *) +.syne +.desc +The +.code hash-zip +function constructs a hash as if by a call to +.mono +.meti (apply hash << hash-args ), +.onom +where +.meta hash-args +is a list of the individual +.meta hash-arg +variadic arguments. + +The hash is then populated with keys taken from +.meta key-seq +which are paired with values taken from from +.metn value-seq , +and returned. + +If +.meta key-seq +is longer than +.metn value-seq , +then the excess keys are ignored, and +.IR "vice versa" . + .coNP Function @ hash-update .synb .mets (hash-update < hash << function ) |