lisp - 嵌套 alist 到 plist 与 alexandria

标签 lisp common-lisp

Alexandria 非常擅长将简单的 alist 转换为 plist,例如:

> (get-document "flower-photo-with-lytro")

((:|_id| . "flower-photo-with-lytro")
 (:|_rev| . "6-eb6d9b71251c167039d3e73d8c0c9a63")
 (:TITLE . "flower-photo-with-lytro")
 (:AUTHOR . "devnull") 
 (:TEXT . "hello here is a  sample flower .... ")
 (:TIME . "3558566236"))


> (alexandria:alist-plist (get-document "flower-photo-with-lytro"))

(:|_id| "flower-photo-with-lytro" :|_rev| "6-eb6d9b71251c167039d3e73d8c0c9a63" 
 :TITLE   "flower-photo-with-lytro" :AUTHOR "devnull"
 :TEXT "hello here is a sample flower .... " :TIME "3558566236")

我如何使用 alexandria 来格式化更结构化的列表,例如

> (invoke-view "hulk" "time")

((:|total_rows| . 2)
 (:|offset| . 0)
 (:|rows|
  ((:|id| . "flower-photo-with-lytro")
   (:|key| . "3558566236")
   (:|value| . "flower-photo-with-lytro"))
  ((:|id| . "hello-world-in-common-lisp-and-restas")
   (:|key| . "3558567019")
   (:|value| . "3558567019-hello-world-in-common-lisp-and-restas"))))

获取带有 :id、:key 和 :value 的 plist?

最佳答案

(defparameter *test*
  '((:|total_rows| . 2)
    (:|offset| . 0)
    (:|rows|
     ((:|id| . "flower-photo-with-lytro")
      (:|key| . "3558566236")
      (:|value| . "flower-photo-with-lytro"))
     ((:|id| . "hello-world-in-common-lisp-and-restas")
      (:|key| . "3558567019")
      (:|value| . "3558567019-hello-world-in-common-lisp-and-restas")))))

(defun rows-alist (tree)
  (list (car tree)
        (cadr tree)
        (alexandria:flatten (caddr tree))))

好吧,我会尝试猜测......


如果您想重命名关键字:

(defun rows-alist (tree)
  (list (car tree)
        (cadr tree)
        (mapcar
         #'(lambda (x)
             (if (symbolp x)
                 (intern
                  (string-upcase (symbol-name x))
                  "KEYWORD") x))
         (alexandria:flatten (caddr tree)))))

关于lisp - 嵌套 alist 到 plist 与 alexandria,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12771215/

相关文章:

list - 在 Lisp 中更改列表的副本而不更改原始列表

clojure - 在 Clojure 中建模游戏 UI 屏幕

emacs - SLIME who-用 'nesting exceeds max-lisp-eval-depth' 报错

Lisp 重新定义函数

macros - Common Lisp 宏的 Catch-22 情况

common-lisp - 如何从 Common Lisp 中的函数符号中获取函数表达式?

string - 字符串处理中Lisp性能优化方案

math - CLISP 中自然对数的精度不正确。可能出了什么问题?

macros - Lisp 宏在我不希望它时评估表达式

lisp - 将值插入结构以在不修改结构的情况下进行测试