From f33c6cfa52c7cdcd5c23fee9e4254ff9fae2779e Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 13 Feb 2019 06:24:25 -0800 Subject: hash-from-alist: new function. * hash.c (hash_from_alist_v): New function. (hash_init): Register hash-from-alist intrinsic. * hash.h (hash_from_alist_v): Declared. * txr.1: Documented. --- hash.c | 15 +++++++++++++++ hash.h | 1 + txr.1 | 18 +++++++++++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/hash.c b/hash.c index 272e1e2f..6babca4a 100644 --- a/hash.c +++ b/hash.c @@ -1195,6 +1195,20 @@ val hash_from_pairs_v(val pairs, struct args *hashv_args) return hash_construct(args_get_list(hashv_args), pairs); } +val hash_from_alist_v(val alist, struct args *hashv_args) +{ + val hash = hashv(hashv_args); + + alist = nullify(alist); + + for (; alist; alist = cdr(alist)) { + val pair = car(alist); + sethash(hash, car(pair), cdr(pair)); + } + + return hash; +} + val hash_list(val keys, struct args *hashv_args) { val hash = hashv(hashv_args); @@ -1598,6 +1612,7 @@ void hash_init(void) reg_fun(intern(lit("hash"), user_package), func_n0v(hashv)); reg_fun(hash_construct_s, func_n2(hash_construct)); 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("gethash"), user_package), func_n3o(gethash_n, 2)); reg_fun(intern(lit("inhash"), user_package), func_n3o(inhash, 2)); diff --git a/hash.h b/hash.h index 6b65daf0..ed94d77a 100644 --- a/hash.h +++ b/hash.h @@ -55,6 +55,7 @@ val hashv(struct args *args); val hashl(val args); 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 group_by(val func, val seq, struct args *hashv_args); val group_reduce(val hash, val by_fun, val reduce_fun, val seq, diff --git a/txr.1 b/txr.1 index 7118b588..658fbad0 100644 --- a/txr.1 +++ b/txr.1 @@ -40769,10 +40769,11 @@ function is affected or its hash value under is altered, the behavior of subsequent lookup and insertion operations on the becomes unspecified. -.coNP Functions @ hash-construct and @ hash-from-pairs +.coNP Functions @, hash-construct @ hash-from-pairs and @ hash-from-alist .synb .mets (hash-construct < hash-args << key-val-pairs ) .mets (hash-from-pairs < key-val-pairs << hash-arg *) +.mets (hash-from-alist < alist << hash-arg *) .syne .desc The @@ -40800,6 +40801,21 @@ argument is first, and the .meta hash-args are passed as trailing variadic arguments, rather than a single list argument. +The +.code hash-from-alist +function is similar to +.codn hash-from-pairs , +except that the +.meta alist +argument specifies they keys and values as an association list. +The elements of the list are +.code cons +cells, each of whose +.code car +is a key, and whose +.code cdr +is the value. + .coNP Function @ hash-list .synb .mets (hash-list < key-list << hash-arg *) -- cgit v1.2.3