diff options
-rw-r--r-- | ChangeLog | 11 | ||||
-rw-r--r-- | eval.c | 2 | ||||
-rw-r--r-- | hash.c | 19 | ||||
-rw-r--r-- | hash.h | 2 | ||||
-rw-r--r-- | txr.1 | 36 |
5 files changed, 68 insertions, 2 deletions
@@ -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. @@ -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)); @@ -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); @@ -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); @@ -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 ) |