summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2015-06-24 06:04:43 -0700
committerKaz Kylheku <kaz@kylheku.com>2015-06-24 06:04:43 -0700
commit5a1a9076e10d03a070daa0e8c019fc0ec76e5c88 (patch)
treea57fb2920b46506fd80130a798e59389f8ea9ee5
parent2f654aa0a794581bb857c38e99e0abb185c53d52 (diff)
downloadtxr-5a1a9076e10d03a070daa0e8c019fc0ec76e5c88.tar.gz
txr-5a1a9076e10d03a070daa0e8c019fc0ec76e5c88.tar.bz2
txr-5a1a9076e10d03a070daa0e8c019fc0ec76e5c88.zip
* hash.c (hash_from_pairs, hash_list): New functions.
* hash.h (hash_from_pairs, hash_list): Declared. * eval.c (eval_init): Registered hash-from-pairs and hash-list intrinsic. * txr.1: Documented new functions.
-rw-r--r--ChangeLog11
-rw-r--r--eval.c2
-rw-r--r--hash.c19
-rw-r--r--hash.h2
-rw-r--r--txr.136
5 files changed, 68 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index 99614ffb..ed057830 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,16 @@
2015-06-24 Kaz Kylheku <kaz@kylheku.com>
+ * hash.c (hash_from_pairs, hash_list): New functions.
+
+ * hash.h (hash_from_pairs, hash_list): Declared.
+
+ * eval.c (eval_init): Registered hash-from-pairs and hash-list
+ intrinsic.
+
+ * txr.1: Documented new functions.
+
+2015-06-24 Kaz Kylheku <kaz@kylheku.com>
+
Refactoring n-ary functions to use a single helper.
* lib.c (nary_op): New function.
diff --git a/eval.c b/eval.c
index c61b1460..1b9b7805 100644
--- a/eval.c
+++ b/eval.c
@@ -4251,6 +4251,8 @@ void eval_init(void)
reg_fun(intern(lit("copy-hash"), user_package), func_n1(copy_hash));
reg_fun(intern(lit("hash"), user_package), func_n0v(hashv));
reg_fun(intern(lit("hash-construct"), user_package), func_n2(hash_construct));
+ reg_fun(intern(lit("hash-from-pairs"), user_package), func_n1v(hash_from_pairs));
+ reg_fun(intern(lit("hash-list"), user_package), func_n1v(hash_list));
reg_fun(gethash_s, 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));
diff --git a/hash.c b/hash.c
index 45e18298..4ece8a54 100644
--- a/hash.c
+++ b/hash.c
@@ -880,6 +880,25 @@ val hash_construct(val hashv_args, val pairs)
return hash;
}
+val hash_from_pairs(val pairs, val hashv_args)
+{
+ return hash_construct(default_bool_arg(hashv_args), pairs);
+}
+
+val hash_list(val keys, val hashv_args)
+{
+ val hash = hashv(default_bool_arg(hashv_args));
+
+ keys = nullify(keys);
+
+ for (; keys; keys = cdr(keys)) {
+ val key = car(keys);
+ sethash(hash, key, key);
+ }
+
+ return hash;
+}
+
val group_by(val func, val seq, val hashv_args)
{
val hash = hashv(hashv_args);
diff --git a/hash.h b/hash.h
index 6b393b77..a732698a 100644
--- a/hash.h
+++ b/hash.h
@@ -48,6 +48,8 @@ val hash_eql(val obj);
val hash_equal(val obj);
val hashv(val args);
val hash_construct(val hashv_args, val pairs);
+val hash_from_pairs(val pairs, val hashv_args);
+val hash_list(val keys, val hashv_args);
val group_by(val func, val seq, val hashv_args);
val hash_keys(val hash);
val hash_values(val hash);
diff --git a/txr.1 b/txr.1
index 3a26ad89..c9501c22 100644
--- a/txr.1
+++ b/txr.1
@@ -21723,9 +21723,10 @@ is fully instantiated. In the meanwhile, the
\*(TX program can mutate the hash table from which the lazy list
is being generated.
-.coNP Function @ hash-construct
+.coNP Functions @ hash-construct and @ hash-from-pairs
.synb
.mets (hash-construct < hash-args << key-val-pairs )
+.mets (hash-from-pairs < key-val-pairs << hash-arg *)
.syne
.desc
The
@@ -21740,11 +21741,42 @@ lists representing key-value pairs.
A hash is constructed as if by a call to
.cblk
-.meti [apply hash << hash-args ],
+.meti (apply hash << hash-args ),
.cble
then populated
with the specified pairs, and returned.
+The
+.code hash-from-pairs
+function is an alternative interface to the same semantics. The
+.meta key-val-pairs
+argument is first, and the
+.meta hash-args
+are passed as trailing variadic arguments, rather than a single list argument.
+
+.coNP Function @ hash-list
+.synb
+.mets (hash-list < key-list << hash-arg *)
+.syne
+.desc
+The
+.code hash-list
+function constructs a hash as if by a call to
+.cblk
+.meti (apply hash << hash-args ),
+.cble
+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-list
+and returned.
+
+The value associated with each key is that key itself.
+
.coNP Function @ hash-update
.synb
.mets (hash-update < hash << function )