diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2021-12-22 07:21:26 -0800 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2021-12-22 07:21:26 -0800 |
commit | e2ff7c1811deb19d0dab2142b99cfcb4b30e62df (patch) | |
tree | 25dcd25d5a48f60fc8c372865329027248268422 | |
parent | 638293d5281db1276a3b64f2eb969ad99c0ee9f3 (diff) | |
download | txr-e2ff7c1811deb19d0dab2142b99cfcb4b30e62df.tar.gz txr-e2ff7c1811deb19d0dab2142b99cfcb4b30e62df.tar.bz2 txr-e2ff7c1811deb19d0dab2142b99cfcb4b30e62df.zip |
The pairlis function comes to TXR Lisp.
* eval.c (eval_init): Register pairlis intrinsic.
* lib.c, lib.h (pairlis): New function.
* tests/012/seq.tl: New test cases.
* txr.1: Documented.
* stdlib/doc-syms.tl: Updated.
-rw-r--r-- | eval.c | 1 | ||||
-rw-r--r-- | lib.c | 19 | ||||
-rw-r--r-- | lib.h | 1 | ||||
-rw-r--r-- | stdlib/doc-syms.tl | 1 | ||||
-rw-r--r-- | tests/012/seq.tl | 8 | ||||
-rw-r--r-- | txr.1 | 57 |
6 files changed, 87 insertions, 0 deletions
@@ -7277,6 +7277,7 @@ void eval_init(void) reg_fun(intern(lit("copy-cons"), user_package), func_n1(copy_cons)); reg_fun(intern(lit("copy-tree"), user_package), func_n1(copy_tree)); reg_fun(intern(lit("copy-alist"), user_package), func_n1(copy_alist)); + reg_fun(intern(lit("pairlis"), user_package), func_n3o(pairlis, 2)); reg_fun(intern(lit("prop"), user_package), func_n2(getplist)); reg_fun(intern(lit("memp"), user_package), func_n2(memp)); reg_fun(intern(lit("plist-to-alist"), user_package), func_n1(plist_to_alist)); @@ -10103,6 +10103,25 @@ val copy_alist(val list) return out; } +val pairlis(val keys, val values, val alist_in) +{ + val self = lit("pairlis"); + val alist = default_null_arg(alist_in); + seq_iter_t sik, siv; + val key, value; + list_collect_decl (out, ptail); + + seq_iter_init(self, &sik, keys); + seq_iter_init(self, &siv, values); + + while (seq_get(&sik, &key) && seq_get(&siv, &value)) + ptail = list_collect(ptail, cons(key, value)); + + list_collect_nconc(ptail, alist); + + return out; +} + val mapcar_listout(val fun, val seq) { val self = lit("mapcar"); @@ -1164,6 +1164,7 @@ val alist_nremove1(val list, val key); val copy_cons(val cons); val copy_tree(val tree); val copy_alist(val list); +val pairlis(val keys, val values, val alist_in); val mapcar_listout(val fun, val seq); val mapcar(val fun, val seq); val mapcon(val fun, val list); diff --git a/stdlib/doc-syms.tl b/stdlib/doc-syms.tl index 6be397ea..63894b85 100644 --- a/stdlib/doc-syms.tl +++ b/stdlib/doc-syms.tl @@ -1399,6 +1399,7 @@ ("package-symbols" "N-03AF0206") ("packagep" "N-007A478F") ("pad" "N-0247F5FA") + ("pairlis" "N-02EA9B54") ("parenb" "N-01B1B5DF") ("parmrk" "N-02391683") ("parodd" "N-01B1B5DF") diff --git a/tests/012/seq.tl b/tests/012/seq.tl index ed2e02b8..1bc45b55 100644 --- a/tests/012/seq.tl +++ b/tests/012/seq.tl @@ -437,3 +437,11 @@ (mtest [subst "brown" "black" #("how" "now" "BROWN" "cow") : downcase-str] #("how" "now" "black" "cow") [subst 5 0 '(1 2 3 4 5 6 7 8 9 10) <] (1 2 3 4 5 0 0 0 0 0)) + +(mtest + (pairlis nil nil) nil + (pairlis "abc" #(1 2 3 4)) ((#\a . 1) (#\b . 2) (#\c . 3)) + (pairlis "abcd" #(1 2 3)) ((#\a . 1) (#\b . 2) (#\c . 3)) + (pairlis "" #(1 2 3)) nil + (pairlis "abcd" #()) nil + (pairlis '(1 2 3) '(a b c) '(4 5 6)) ((1 . a) (2 . b) (3 . c) 4 5 6)) @@ -22710,6 +22710,63 @@ is produced as if by the function applied to the corresponding element of the input list. +.coNP Function @ pairlis +.synb +.mets (pairlis < keys < values <> [ alist ]) +.syne +.desc +The +.code pairlis +function returns a association list consisting of pairs formed from +the elements of +.meta keys +and +.meta values +prepended to the existing +.metn alist . + +If an +.meta alist +argument is omitted, it defaults to +.codn nil . + +Pairs of elements are formed by taking successive elements from the +.meta keys +and +.meta values +sequences in parallel. + +If the sequences are not of equal length, the excess elements from +the longer sequence are ignored. + +The pairs appear in the resulting list in the original order in +which their constituents appeared in +.meta keys +and +.metn values . + +.TP* "Dialect Note:" +The ANSI CL +.code pairlis +requires +.meta key +and +.meta data +to be lists, not sequences. The behavior of the ANSI CL +.code pairlis +is undefined of those lists are of different lengths. Finally, the elements are +permitted to appear in either the original order or reverse order. + +.TP* Examples: + +.verb + (pairlis nil nil) -> nil + (pairlis "abc" #(1 2 3 4)) -> ((#\ea . 1) (#\eb . 2) (#\ec . 3)) + + (pairlis '(1 2 3) '(a b c) '((x . y) (z . w))) + -> ((1 . a) (2 . b) (3 . c) (x . y) (z . w)) +.brev + .SS* Property Lists A .IR "property list", |