Skip to content

Instantly share code, notes, and snippets.

@kosh04
Created May 25, 2009 16:30
Show Gist options
  • Save kosh04/117605 to your computer and use it in GitHub Desktop.
Save kosh04/117605 to your computer and use it in GitHub Desktop.
(defun nalist-to-plist (list)
"連想リストをプロパティリストに変換する[破壊的]."
(if (null list)
nil
(let ((car (car list))
(cdr (cdr list)))
(rplaca list (car car))
(rplacd list car)
(rplaca (cdr list) (cdr car))
(rplacd (cdr list) cdr)
(nalist-to-plist (nthcdr 2 list))
list)))
(nalist-to-plist '((foo . 1) (bar . 2) (baz . 3)))
;;=> (foo 1 bar 2 baz 3)
(prog ((data (loop :repeat 10000 :collect (cons 1 2))))
(time (nalist-to-plist data)))
;;=> NIL
#|
Evaluation took:
0.001 seconds of real time
0.000000 seconds of total run time (0.000000 user, 0.000000 system)
0.00% CPU
853,799 processor cycles
0 bytes consed
~~~~~~~~~~~~~~
|#
(defun nplist-to-alist (list)
"プロパティリストを連想リストに変換する[破壊的]."
(if (null (cdr list))
nil
(let ((x (car list))
(y (car (cdr list))))
(rplaca list (cdr list))
(rplacd list (cdr (cdr list)))
(rplaca (car list) x)
(rplacd (car list) y)
(nplist-to-alist (cdr list))
list)))
(nplist-to-alist '(foo 1 bar 2 baz 3))
;;=> ((foo . 1) (bar . 2) (baz . 3))
(prog ((data (loop :repeat 100000 :collect (cons 1 2))))
(time (nplist-to-alist data)))
;;=> NIL
#|
Evaluation took:
0.006 seconds of real time
0.004000 seconds of total run time (0.004000 user, 0.000000 system)
66.67% CPU
7,906,917 processor cycles
0 bytes consed
~~~~~~~~~~~~~~
|#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment