summaryrefslogtreecommitdiffstats
path: root/hash.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2023-05-01 11:40:09 -0700
committerKaz Kylheku <kaz@kylheku.com>2023-05-01 11:40:09 -0700
commitcb9b5f6e8ffbec96b5c22ab89bc6852f27dd29b8 (patch)
tree86a3ba7dda9ae267a6da0efbc872b01b60a57517 /hash.c
parent5f2d243892ed9affeba7a842e6d184046c9e8420 (diff)
downloadtxr-cb9b5f6e8ffbec96b5c22ab89bc6852f27dd29b8.tar.gz
txr-cb9b5f6e8ffbec96b5c22ab89bc6852f27dd29b8.tar.bz2
txr-cb9b5f6e8ffbec96b5c22ab89bc6852f27dd29b8.zip
hash: new function, hash-props.
We don't have a function in the hash table module which can create a populated hash table in one step without requiring the caller to create auxiliary lists. This new function fills that gap, albeit with some limitations. * hash.c (hash_props): New function. (hash_init): Register hash-props intrinsic. * tests/010/hash.tl: New tests. * txr.1: Documented. * stdlib/doc-syms.tl: Updated.
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/hash.c b/hash.c
index 1b85e01f..f258bcb7 100644
--- a/hash.c
+++ b/hash.c
@@ -1613,6 +1613,26 @@ val hash_from_alist_v(val alist, struct args *hashv_args)
return hash;
}
+val hash_props(struct args *plist)
+{
+ val self = lit("hash-props");
+ args_decl_constsize(args, ARGS_MIN);
+ val hash = hashv(args);
+ cnum index = 0;
+
+ while (args_two_more(plist, index)) {
+ val key = args_get(plist, &index);
+ val value = args_get(plist, &index);
+ sethash(hash, key, value);
+ }
+
+ if (args_more(plist, index))
+ uw_throwf(error_s, lit("~a: unpaired ~s argument"),
+ self, args_get(plist, &index), nao);
+
+ return hash;
+}
+
val hash_list(val keys, struct args *hashv_args)
{
val hash = hashv(hashv_args);
@@ -2121,6 +2141,7 @@ void hash_init(void)
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-props"), user_package), func_n0v(hash_props));
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));