2

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?

4

1 回答 1

2
(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)))))
于 2012-10-07T20:03:57.600 回答