1 (in-package :alexandria
)
3 (declaim (inline safe-endp
))
5 (declare (optimize safety
))
8 (defun alist-plist (alist)
9 "Returns a property list containing the same keys and values as the
10 association list ALIST in the same order."
13 (push (car pair
) plist
)
14 (push (cdr pair
) plist
))
17 (defun plist-alist (plist)
18 "Returns an association list containing the same keys and values as the
19 property list PLIST in the same order."
21 (do ((tail plist
(cddr tail
)))
22 ((safe-endp tail
) (nreverse alist
))
23 (push (cons (car tail
) (cadr tail
)) alist
))))
25 (declaim (inline racons
))
26 (defun racons (key value ralist
)
27 (acons value key ralist
))
30 ((define-alist-get (name get-entry get-value-from-entry add doc
)
32 (declaim (inline ,name
))
33 (defun ,name
(alist key
&key
(test 'eql
))
35 (let ((entry (,get-entry key alist
:test test
)))
36 (values (,get-value-from-entry entry
) entry
)))
37 (define-setf-expander ,name
(place key
&key
(test ''eql
)
40 (temporary-variables initforms newvals setter getter
)
41 (get-setf-expansion place env
)
43 (error "~A cannot store multiple values in one place" ',name
))
44 (with-unique-names (new-value key-val test-val alist entry
)
46 (append temporary-variables
55 `(,',get-entry
,key-val
,alist
:test
,test-val
)))
59 (setf (,',get-value-from-entry
,entry
) ,new-value
))
62 (setf ,(first newvals
) (,',add
,key
,new-value
,alist
))
65 `(,',get-value-from-entry
,entry
))))))))
66 (define-alist-get assoc-value assoc cdr acons
67 "ASSOC-VALUE is an alist accessor very much like ASSOC, but it can
69 (define-alist-get rassoc-value rassoc car racons
70 "RASSOC-VALUE is an alist accessor very much like RASSOC, but it can
73 (defun malformed-plist (plist)
74 (error "Malformed plist: ~S" plist
))
76 (defmacro doplist
((key val plist
&optional values
) &body body
)
77 "Iterates over elements of PLIST. BODY can be preceded by
78 declarations, and is like a TAGBODY. RETURN may be used to terminate
79 the iteration early. If RETURN is not used, returns VALUES."
80 (multiple-value-bind (forms declarations
) (parse-body body
)
81 (with-gensyms (tail loop results
)
85 (declare (ignorable ,key
,val
))
93 (malformed-plist ',plist
))))
94 (declare (ignorable ,key
,val
))
104 (malformed-plist ',plist
)))
107 (define-modify-macro appendf
(&rest lists
) append
108 "Modify-macro for APPEND. Appends LISTS to the place designated by the first
111 (define-modify-macro nconcf
(&rest lists
) nconc
112 "Modify-macro for NCONC. Concatenates LISTS to place designated by the first
115 (define-modify-macro unionf
(list &rest args
) union
116 "Modify-macro for UNION. Saves the union of LIST and the contents of the
117 place designated by the first argument to the designated place.")
119 (define-modify-macro nunionf
(list &rest args
) nunion
120 "Modify-macro for NUNION. Saves the union of LIST and the contents of the
121 place designated by the first argument to the designated place. May modify
124 (define-modify-macro reversef
() reverse
125 "Modify-macro for REVERSE. Copies and reverses the list stored in the given
126 place and saves back the result into the place.")
128 (define-modify-macro nreversef
() nreverse
129 "Modify-macro for NREVERSE. Reverses the list stored in the given place by
130 destructively modifying it and saves back the result into the place.")
132 (defun circular-list (&rest elements
)
133 "Creates a circular list of ELEMENTS."
134 (let ((cycle (copy-list elements
)))
135 (nconc cycle cycle
)))
137 (defun circular-list-p (object)
138 "Returns true if OBJECT is a circular list, NIL otherwise."
140 (do ((fast object
(cddr fast
))
141 (slow (cons (car object
) (cdr object
)) (cdr slow
)))
143 (unless (and (consp fast
) (listp (cdr fast
)))
148 (defun circular-tree-p (object)
149 "Returns true if OBJECT is a circular tree, NIL otherwise."
150 (labels ((circularp (object seen
)
152 (do ((fast (cons (car object
) (cdr object
)) (cddr fast
))
153 (slow object
(cdr slow
)))
154 ((or (not (consp fast
)) (not (consp (cdr slow
))))
155 (do ((tail object
(cdr tail
)))
158 (let ((elt (car tail
)))
159 (circularp elt
(cons object seen
)))))
160 (when (or (eq fast slow
) (member slow seen
))
161 (return-from circular-tree-p t
))))))
162 (circularp object nil
)))
164 (defun proper-list-p (object)
165 "Returns true if OBJECT is a proper list."
169 (do ((fast object
(cddr fast
))
170 (slow (cons (car object
) (cdr object
)) (cdr slow
)))
172 (unless (and (listp fast
) (consp (cdr fast
)))
173 (return (and (listp fast
) (not (cdr fast
)))))
179 (deftype proper-list
()
180 "Type designator for proper lists. Implemented as a SATISFIES type, hence
181 not recommended for performance intensive use. Main usefullness as a type
182 designator of the expected type in a TYPE-ERROR."
183 `(and list
(satisfies proper-list-p
)))
185 (defun circular-list-error (list)
188 :expected-type
'(and list
(not circular-list
))))
190 (macrolet ((def (name lambda-list doc step declare ret1 ret2
)
191 (assert (member 'list lambda-list
))
192 `(defun ,name
,lambda-list
194 (do ((last list fast
)
195 (fast list
(cddr fast
))
196 (slow (cons (car list
) (cdr list
)) (cdr slow
))
197 ,@(when step
(list step
)))
199 (declare (dynamic-extent slow
) ,@(when declare
(list declare
)))
200 (when (safe-endp fast
)
202 (when (safe-endp (cdr fast
))
205 (circular-list-error list
))))))
206 (def proper-list-length
(list)
207 "Returns length of LIST, signalling an error if it is not a proper list."
209 ;; KLUDGE: Most implementations don't actually support lists with bignum
210 ;; elements -- and this is WAY faster on most implementations then declaring
211 ;; N to be an UNSIGNED-BYTE.
217 "Returns the last element of LIST. Signals a type-error if LIST is not a
224 (def (setf lastcar
) (object list
)
225 "Sets the last element of LIST. Signals a type-error if LIST is not a proper
229 (setf (cadr last
) object
)
230 (setf (car fast
) object
)))
232 (defun make-circular-list (length &key initial-element
)
233 "Creates a circular list of LENGTH with the given INITIAL-ELEMENT."
234 (let ((cycle (make-list length
:initial-element initial-element
)))
235 (nconc cycle cycle
)))
237 (deftype circular-list
()
238 "Type designator for circular lists. Implemented as a SATISFIES type, so not
239 recommended for performance intensive use. Main usefullness as the
240 expected-type designator of a TYPE-ERROR."
241 `(satisfies circular-list-p
))
243 (defun ensure-car (thing)
244 "If THING is a CONS, its CAR is returned. Otherwise THING is returned."
249 (defun ensure-cons (cons)
250 "If CONS is a cons, it is returned. Otherwise returns a fresh cons with CONS
251 in the car, and NIL in the cdr."
256 (defun ensure-list (list)
257 "If LIST is a list, it is returned. Otherwise returns the list designated by LIST."
262 (defun remove-from-plist (plist &rest keys
)
263 "Returns a propery-list with same keys and values as PLIST, except that keys
264 in the list designated by KEYS and values corresponding to them are removed.
265 The returned property-list may share structure with the PLIST, but PLIST is
266 not destructively modified. Keys are compared using EQ."
267 (declare (optimize (speed 3)))
268 ;; FIXME: possible optimization: (remove-from-plist '(:x 0 :a 1 :b 2) :a)
269 ;; could return the tail without consing up a new list.
270 (loop for
(key . rest
) on plist by
#'cddr
271 do
(assert rest
() "Expected a proper plist, got ~S" plist
)
272 unless
(member key keys
:test
#'eq
)
273 collect key and collect
(first rest
)))
275 (defun delete-from-plist (plist &rest keys
)
276 "Just like REMOVE-FROM-PLIST, but this version may destructively modify the
278 ;; FIXME: should not cons
279 (apply 'remove-from-plist plist keys
))
281 (define-modify-macro remove-from-plistf
(&rest keys
) remove-from-plist
282 "Modify macro for REMOVE-FROM-PLIST.")
283 (define-modify-macro delete-from-plistf
(&rest keys
) delete-from-plist
284 "Modify macro for DELETE-FROM-PLIST.")
286 (declaim (inline sans
))
287 (defun sans (plist &rest keys
)
288 "Alias of REMOVE-FROM-PLIST for backward compatibility."
289 (apply #'remove-from-plist plist keys
))
291 (defun mappend (function &rest lists
)
292 "Applies FUNCTION to respective element(s) of each LIST, appending all the
293 all the result list to a single list. FUNCTION must return a list."
294 (loop for results in
(apply #'mapcar function lists
)
297 (defun setp (object &key
(test #'eql
) (key #'identity
))
298 "Returns true if OBJECT is a list that denotes a set, NIL otherwise. A list
299 denotes a set if each element of the list is unique under KEY and TEST."
302 (dolist (elt object t
)
303 (let ((key (funcall key elt
)))
304 (if (member key seen
:test test
)
306 (push key seen
)))))))
308 (defun set-equal (list1 list2
&key
(test #'eql
) (key nil keyp
))
309 "Returns true if every element of LIST1 matches some element of LIST2 and
310 every element of LIST2 matches some element of LIST1. Otherwise returns false."
311 (let ((keylist1 (if keyp
(mapcar key list1
) list1
))
312 (keylist2 (if keyp
(mapcar key list2
) list2
)))
313 (and (dolist (elt keylist1 t
)
314 (or (member elt keylist2
:test test
)
316 (dolist (elt keylist2 t
)
317 (or (member elt keylist1
:test test
)
320 (defun map-product (function list
&rest more-lists
)
321 "Returns a list containing the results of calling FUNCTION with one argument
322 from LIST, and one from each of MORE-LISTS for each combination of arguments.
323 In other words, returns the product of LIST and MORE-LISTS using FUNCTION.
327 (map-product 'list '(1 2) '(3 4) '(5 6))
328 => ((1 3 5) (1 3 6) (1 4 5) (1 4 6)
329 (2 3 5) (2 3 6) (2 4 5) (2 4 6))
331 (labels ((%map-product
(f lists
)
332 (let ((more (cdr lists
))
337 (%map-product
(curry f x
) more
))
339 (%map-product
(ensure-function function
) (cons list more-lists
))))
341 (defun flatten (tree)
342 "Traverses the tree in order, collecting non-null leaves into a list."
344 (labels ((traverse (subtree)
348 (traverse (car subtree
))
349 (traverse (cdr subtree
)))
350 (push subtree list
)))))