diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2019-02-13 06:24:25 -0800 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2019-02-13 06:24:25 -0800 |
commit | dabd00c6f02b73c43cdf8c4ec760092cc5a04268 (patch) | |
tree | 5855d661cc340c184e4c7fce2db61a000f4759ea | |
parent | 0757a921ab7db0f15ea703b5f5b185fa5a035ba6 (diff) | |
download | txr-dabd00c6f02b73c43cdf8c4ec760092cc5a04268.tar.gz txr-dabd00c6f02b73c43cdf8c4ec760092cc5a04268.tar.bz2 txr-dabd00c6f02b73c43cdf8c4ec760092cc5a04268.zip |
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.
-rw-r--r-- | hash.c | 15 | ||||
-rw-r--r-- | hash.h | 1 | ||||
-rw-r--r-- | txr.1 | 18 |
3 files changed, 33 insertions, 1 deletions
@@ -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)); @@ -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, @@ -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 *) |