1 (in-package :alexandria
)
3 (defun alist-plist (alist)
4 "Returns a property list containing the same keys and values as the
5 association list ALIST in the same order."
8 (push (car pair
) plist
)
9 (push (cdr pair
) plist
))
12 (defun plist-alist (plist)
13 "Returns an association list containing the same keys and values as the
14 property list PLIST in the same order."
16 (do ((tail plist
(cddr tail
)))
17 ((endp tail
) (nreverse alist
))
18 (push (cons (car tail
) (cadr tail
)) alist
))))
20 (defun malformed-plist (plist)
21 (error "Malformed plist: ~S" plist
))
23 (defmacro doplist
((key val plist
&optional values
) &body body
)
24 "Iterates over elements of PLIST. BODY can be preceded by
25 declarations, and is like a TAGBODY. RETURN may be used to terminate
26 the iteration early. If RETURN is not used, returns VALUES."
27 (multiple-value-bind (forms declarations
) (parse-body body
)
28 (with-gensyms (tail loop results
)
32 (declare (ignorable ,key
,val
))
40 (malformed-plist ',plist
))))
41 (declare (ignorable ,key
,val
))
51 (malformed-plist ',plist
)))
54 (define-modify-macro appendf
(&rest lists
) append
55 "Modify-macro for APPEND. Appends LISTS to the place designated by the first
58 (define-modify-macro nconcf
(&rest lists
) nconc
59 "Modify-macro for NCONC. Concatenates LISTS to place designated by the first
62 (define-modify-macro unionf
(list) union
63 "Modify-macro for UNION. Saves the union of LIST and the contents of the
64 place designated by the first argument to the designated place.")
66 (define-modify-macro nunionf
(list) nunion
67 "Modify-macro for NUNION. Saves the union of LIST and the contents of the
68 place designated by the first argument to the designated place. May modify
71 (defun circular-list (&rest elements
)
72 "Creates a circular list of ELEMENTS."
73 (let ((cycle (copy-list elements
)))
76 (defun circular-list-p (object)
77 "Returns true if OBJECT is a circular list, NIL otherwise."
79 (do ((fast object
(cddr fast
))
80 (slow (cons (car object
) (cdr object
)) (cdr slow
)))
82 (unless (and (consp fast
) (listp (cdr fast
)))
87 (defun circular-tree-p (object)
88 "Returns true if OBJECT is a circular tree, NIL otherwise."
89 (labels ((circularp (object seen
)
91 (do ((fast (cons (car object
) (cdr object
)) (cddr fast
))
92 (slow object
(cdr slow
)))
93 ((or (not (consp fast
)) (not (consp (cdr slow
))))
94 (do ((tail object
(cdr tail
)))
97 (let ((elt (car tail
)))
98 (circularp elt
(cons object seen
)))))
99 (when (or (eq fast slow
) (member slow seen
))
100 (return-from circular-tree-p t
))))))
101 (circularp object nil
)))
103 (defun proper-list-p (object)
104 "Returns true if OBJECT is a proper list."
108 (do ((fast object
(cddr fast
))
109 (slow (cons (car object
) (cdr object
)) (cdr slow
)))
111 (unless (and (listp fast
) (consp (cdr fast
)))
112 (return (and (listp fast
) (not (cdr fast
)))))
118 (deftype proper-list
()
119 "Type designator for proper lists. Implemented as a SATISFIES type, hence
120 not recommended for performance intensive use. Main usefullness as a type
121 designator of the expected type in a TYPE-ERROR."
122 `(and list
(satisfies proper-list-p
)))
124 (defun lastcar (list)
125 "Returns the last element of LIST. Signals a type-error if LIST is not a
127 (do ((last list fast
)
128 (fast list
(cddr fast
))
129 (slow (cons (car list
) (cdr list
)) (cdr slow
)))
132 (return (cadr last
)))
133 (when (endp (cdr fast
))
138 :expected-type
'(and list
(not circular-list
))))))
140 (defun (setf lastcar
) (object list
)
141 "Sets the last element of LIST. Signals a type-error if LIST is not a proper
143 (do ((last list fast
)
144 (fast list
(cddr fast
))
145 (slow (cons (car list
) (cdr list
)) (cdr slow
)))
148 (return (setf (cadr last
) object
)))
149 (when (endp (cdr fast
))
150 (return (setf (car fast
) object
)))
154 :expected-type
'(and list
(not circular-list
))))))
156 (defun make-circular-list (length &key initial-element
)
157 "Creates a circular list of LENGTH with the given INITIAL-ELEMENT."
158 (let ((cycle (make-list length
:initial-element initial-element
)))
159 (nconc cycle cycle
)))
161 (deftype circular-list
()
162 "Type designator for circular lists. Implemented as a SATISFIES type, so not
163 recommended for performance intensive use. Main usefullness as the
164 expected-type designator of a TYPE-ERROR."
165 `(satisfies circular-list-p
))
167 (defun ensure-car (thing)
168 "If THING is a CONS, its CAR is returned. Otherwise THING is returned."
173 (defun ensure-cons (cons)
174 "If CONS is a cons, it is returned. Otherwise returns a fresh cons with CONS
175 in the car, and NIL in the cdr."
180 (defun ensure-list (list)
181 "If LIST is a list, it is returned. Otherwise returns the list designated by LIST."
186 (defun remove-from-plist (plist &rest keys
)
187 "Returns a propery-list with same keys and values as PLIST, except that keys
188 in the list designated by KEYS and values corresponding to them are removed.
189 The returned property-list may share structure with the PLIST, but PLIST is
190 not destructively modified. Keys are compared using EQ."
191 (declare (optimize (speed 3)))
192 ;; FIXME: possible optimization: (remove-from-plist '(:x 0 :a 1 :b 2) :a)
193 ;; could return the tail without consing up a new list.
194 (loop for
(key . rest
) on plist by
#'cddr
195 do
(assert rest
() "Expected a proper plist, got ~S" plist
)
196 unless
(member key keys
:test
#'eq
)
197 collect key and collect
(first rest
)))
199 (defun delete-from-plist (plist &rest keys
)
200 "Just like REMOVE-FROM-PLIST, but this version may destructively modify the
202 ;; FIXME: should not cons
203 (apply 'remove-from-plist plist keys
))
205 (define-modify-macro remove-from-plistf
(&rest keys
) remove-from-plist
)
206 (define-modify-macro delete-from-plistf
(&rest keys
) delete-from-plist
)
208 (declaim (inline sans
))
209 (defun sans (plist &rest keys
)
210 "Alias of REMOVE-FROM-PLIST for backward compatibility."
211 (apply #'remove-from-plist plist keys
))
213 (defun mappend (function &rest lists
)
214 "Applies FUNCTION to respective element(s) of each LIST, appending all the
215 all the result list to a single list. FUNCTION must return a list."
216 (loop for results in
(apply #'mapcar function lists
)
219 (defun setp (object &key
(test #'eql
) (key #'identity
))
220 "Returns true if OBJECT is a list that denotes a set, NIL otherwise. A list
221 denotes a set if each element of the list is unique under KEY and TEST."
224 (dolist (elt object t
)
225 (let ((key (funcall key elt
)))
226 (if (member key seen
:test test
)
228 (push key seen
)))))))
230 (defun set-equal (list1 list2
&key
(test #'eql
) (key nil keyp
))
231 "Returns true if every element of LIST1 matches some element of LIST2 and
232 every element of LIST2 matches some element of LIST1. Otherwise returns false."
233 (let ((keylist1 (if keyp
(mapcar key list1
) list1
))
234 (keylist2 (if keyp
(mapcar key list2
) list2
)))
235 (and (dolist (elt keylist1 t
)
236 (or (member elt keylist2
:test test
)
238 (dolist (elt keylist2 t
)
239 (or (member elt keylist1
:test test
)
242 (defun map-product (function list
&rest more-lists
)
243 "Returns a list containing the results of calling FUNCTION with one argument
244 from LIST, and one from each of MORE-LISTS for each combination of arguments.
245 In other words, returns the product of LIST and MORE-LISTS using FUNCTION.
249 (map-product 'list '(1 2) '(3 4) '(5 6)) => ((1 3 5) (1 3 6) (1 4 5) (1 4 6)
250 (2 3 5) (2 3 6) (2 4 5) (2 4 6))
252 (labels ((%map-product
(f lists
)
253 (let ((more (cdr lists
))
258 (%map-product
(curry f x
) more
))
260 (%map-product
(if (functionp function
)
262 (fdefinition function
))
263 (cons list more-lists
))))
265 (defun flatten (tree)
266 "Traverses the tree in order, collecting non-null leaves into a list."
268 (labels ((traverse (subtree)
272 (traverse (car subtree
))
273 (traverse (cdr subtree
)))
274 (push subtree list
)))))