この連想リストのリストを変換したい:
(setq terms '((("name" . "t1c1")
("taxonomy" . "category"))
(("name" . "t1c2")
("taxonomy" . "category"))
(("name" . "t1k1")
("taxonomy" . "post_tag"))
(("name" . "t1k2")
("taxonomy" . "post_tag"))
(("name" . "t1k3")
("taxonomy" . "post_tag"))))
この他の連想リストのリストに:
(("category" "t1c1" "t1c2")
("post_tag" "t1k1" "t1k2" "t1k3"))
私は思いついた:
(reduce
'(lambda (lists term)
(let* ((name (cdr (assoc "name" term)))
(taxonomy (cdr (assoc "taxonomy" term)))
(existing (assoc taxonomy lists)))
(if existing
(progn
(setcdr existing (sort (cons name (cdr existing)) 'string<)))
(push (list taxonomy name) lists)))
lists)
terms
:initial-value nil)
これは洗練されていないように思えます --- let* と if ステートメントの使用は、どちらも潜在的なコードの匂いのように思えます。
elispでこれを行うためのより良い方法の例をいただければ幸いです---より良いとは、より純粋に機能的で、組み込み関数をより適切に使用して特定の操作を表現することなどを意味します.
ああ、結果の連想リストの内容をソートしたいと思います---出力をテストしやすくします。