【问题标题】:nested alist to plist with alexandria嵌套 alist 到 plist 与 alexandria
【发布时间】:2012-10-07 17:41:43
【问题描述】:

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 来格式化更结构化的 alist,例如

> (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?

【问题讨论】:

  • 遗憾地看到有些人浏览 common-lisp 只是为了投票否决问题!
  • 问题是 ((:|total_rows| . 2) (:|offset| . 0) (:|rows| 希望输出一个带有 id 键和值的 plist :) :ID :KEY :价值

标签: lisp common-lisp


【解决方案1】:
(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)))))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-20
    • 1970-01-01
    • 2017-07-06
    相关资源
    最近更新 更多