devnull
devnull

Reputation: 2872

nested alist to plist with alexandria

Alexandria has been great to convert a simple alist to a plist, e.g.:

> (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")

How can I use alexandria to format a more structured alist like

> (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"))))

to obtain a plist with :id, :key and :value?

Upvotes: 2

Views: 555

Answers (1)

user797257
user797257

Reputation:

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

Well, I'll try to guess...


And if you wanted to rename the keywords:

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

Upvotes: 2

Related Questions