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 (defun malformed-plist (plist)
26 (error "Malformed plist: ~S" plist
))
28 (defmacro doplist
((key val plist
&optional values
) &body body
)
29 "Iterates over elements of PLIST. BODY can be preceded by
30 declarations, and is like a TAGBODY. RETURN may be used to terminate
31 the iteration early. If RETURN is not used, returns VALUES."
32 (multiple-value-bind (forms declarations
) (parse-body body
)
33 (with-gensyms (tail loop results
)
37 (declare (ignorable ,key
,val
))
45 (malformed-plist ',plist
))))
46 (declare (ignorable ,key
,val
))
56 (malformed-plist ',plist
)))
59 (define-modify-macro appendf
(&rest lists
) append
60 "Modify-macro for APPEND. Appends LISTS to the place designated by the first
63 (define-modify-macro nconcf
(&rest lists
) nconc
64 "Modify-macro for NCONC. Concatenates LISTS to place designated by the first
67 (define-modify-macro unionf
(list) union
68 "Modify-macro for UNION. Saves the union of LIST and the contents of the
69 place designated by the first argument to the designated place.")
71 (define-modify-macro nunionf
(list) nunion
72 "Modify-macro for NUNION. Saves the union of LIST and the contents of the
73 place designated by the first argument to the designated place. May modify
76 (define-modify-macro reversef
() reverse
77 "Modify-macro for REVERSE. Copies and reverses the list stored in the given
78 place and saves back the result into the place.")
80 (define-modify-macro nreversef
() nreverse
81 "Modify-macro for NREVERSE. Reverses the list stored in the given place by
82 destructively modifying it and saves back the result into the place.")
84 (defun circular-list (&rest elements
)
85 "Creates a circular list of ELEMENTS."
86 (let ((cycle (copy-list elements
)))
89 (defun circular-list-p (object)
90 "Returns true if OBJECT is a circular list, NIL otherwise."
92 (do ((fast object
(cddr fast
))
93 (slow (cons (car object
) (cdr object
)) (cdr slow
)))
95 (unless (and (consp fast
) (listp (cdr fast
)))
100 (defun circular-tree-p (object)
101 "Returns true if OBJECT is a circular tree, NIL otherwise."
102 (labels ((circularp (object seen
)
104 (do ((fast (cons (car object
) (cdr object
)) (cddr fast
))
105 (slow object
(cdr slow
)))
106 ((or (not (consp fast
)) (not (consp (cdr slow
))))
107 (do ((tail object
(cdr tail
)))
110 (let ((elt (car tail
)))
111 (circularp elt
(cons object seen
)))))
112 (when (or (eq fast slow
) (member slow seen
))
113 (return-from circular-tree-p t
))))))
114 (circularp object nil
)))
116 (defun proper-list-p (object)
117 "Returns true if OBJECT is a proper list."
121 (do ((fast object
(cddr fast
))
122 (slow (cons (car object
) (cdr object
)) (cdr slow
)))
124 (unless (and (listp fast
) (consp (cdr fast
)))
125 (return (and (listp fast
) (not (cdr fast
)))))
131 (deftype proper-list
()
132 "Type designator for proper lists. Implemented as a SATISFIES type, hence
133 not recommended for performance intensive use. Main usefullness as a type
134 designator of the expected type in a TYPE-ERROR."
135 `(and list
(satisfies proper-list-p
)))
137 (defun circular-list-error (list)
140 :expected-type
'(and list
(not circular-list
))))
142 (macrolet ((def (name lambda-list doc step declare ret1 ret2
)
143 (assert (member 'list lambda-list
))
144 `(defun ,name
,lambda-list
146 (do ((last list fast
)
147 (fast list
(cddr fast
))
148 (slow (cons (car list
) (cdr list
)) (cdr slow
))
149 ,@(when step
(list step
)))
151 (declare (dynamic-extent slow
) ,@(when declare
(list declare
)))
152 (when (safe-endp fast
)
154 (when (safe-endp (cdr fast
))
157 (circular-list-error list
))))))
158 (def proper-list-length
(list)
159 "Returns length of LIST, signalling an error if it is not a proper list."
161 ;; KLUDGE: Most implementations don't actually support lists with bignum
162 ;; elements -- and this is WAY faster on most implementations then declaring
163 ;; N to be an UNSIGNED-BYTE.
169 "Returns the last element of LIST. Signals a type-error if LIST is not a
176 (def (setf lastcar
) (object list
)
177 "Sets the last element of LIST. Signals a type-error if LIST is not a proper
181 (setf (cadr last
) object
)
182 (setf (car fast
) object
)))
184 (defun make-circular-list (length &key initial-element
)
185 "Creates a circular list of LENGTH with the given INITIAL-ELEMENT."
186 (let ((cycle (make-list length
:initial-element initial-element
)))
187 (nconc cycle cycle
)))
189 (deftype circular-list
()
190 "Type designator for circular lists. Implemented as a SATISFIES type, so not
191 recommended for performance intensive use. Main usefullness as the
192 expected-type designator of a TYPE-ERROR."
193 `(satisfies circular-list-p
))
195 (defun ensure-car (thing)
196 "If THING is a CONS, its CAR is returned. Otherwise THING is returned."
201 (defun ensure-cons (cons)
202 "If CONS is a cons, it is returned. Otherwise returns a fresh cons with CONS
203 in the car, and NIL in the cdr."
208 (defun ensure-list (list)
209 "If LIST is a list, it is returned. Otherwise returns the list designated by LIST."
214 (defun remove-from-plist (plist &rest keys
)
215 "Returns a propery-list with same keys and values as PLIST, except that keys
216 in the list designated by KEYS and values corresponding to them are removed.
217 The returned property-list may share structure with the PLIST, but PLIST is
218 not destructively modified. Keys are compared using EQ."
219 (declare (optimize (speed 3)))
220 ;; FIXME: possible optimization: (remove-from-plist '(:x 0 :a 1 :b 2) :a)
221 ;; could return the tail without consing up a new list.
222 (loop for
(key . rest
) on plist by
#'cddr
223 do
(assert rest
() "Expected a proper plist, got ~S" plist
)
224 unless
(member key keys
:test
#'eq
)
225 collect key and collect
(first rest
)))
227 (defun delete-from-plist (plist &rest keys
)
228 "Just like REMOVE-FROM-PLIST, but this version may destructively modify the
230 ;; FIXME: should not cons
231 (apply 'remove-from-plist plist keys
))
233 (define-modify-macro remove-from-plistf
(&rest keys
) remove-from-plist
)
234 (define-modify-macro delete-from-plistf
(&rest keys
) delete-from-plist
)
236 (declaim (inline sans
))
237 (defun sans (plist &rest keys
)
238 "Alias of REMOVE-FROM-PLIST for backward compatibility."
239 (apply #'remove-from-plist plist keys
))
241 (defun mappend (function &rest lists
)
242 "Applies FUNCTION to respective element(s) of each LIST, appending all the
243 all the result list to a single list. FUNCTION must return a list."
244 (loop for results in
(apply #'mapcar function lists
)
247 (defun setp (object &key
(test #'eql
) (key #'identity
))
248 "Returns true if OBJECT is a list that denotes a set, NIL otherwise. A list
249 denotes a set if each element of the list is unique under KEY and TEST."
252 (dolist (elt object t
)
253 (let ((key (funcall key elt
)))
254 (if (member key seen
:test test
)
256 (push key seen
)))))))
258 (defun set-equal (list1 list2
&key
(test #'eql
) (key nil keyp
))
259 "Returns true if every element of LIST1 matches some element of LIST2 and
260 every element of LIST2 matches some element of LIST1. Otherwise returns false."
261 (let ((keylist1 (if keyp
(mapcar key list1
) list1
))
262 (keylist2 (if keyp
(mapcar key list2
) list2
)))
263 (and (dolist (elt keylist1 t
)
264 (or (member elt keylist2
:test test
)
266 (dolist (elt keylist2 t
)
267 (or (member elt keylist1
:test test
)
270 (defun map-product (function list
&rest more-lists
)
271 "Returns a list containing the results of calling FUNCTION with one argument
272 from LIST, and one from each of MORE-LISTS for each combination of arguments.
273 In other words, returns the product of LIST and MORE-LISTS using FUNCTION.
277 (map-product 'list '(1 2) '(3 4) '(5 6)) => ((1 3 5) (1 3 6) (1 4 5) (1 4 6)
278 (2 3 5) (2 3 6) (2 4 5) (2 4 6))
280 (labels ((%map-product
(f lists
)
281 (let ((more (cdr lists
))
286 (%map-product
(curry f x
) more
))
288 (%map-product
(ensure-function function
) (cons list more-lists
))))
290 (defun flatten (tree)
291 "Traverses the tree in order, collecting non-null leaves into a list."
293 (labels ((traverse (subtree)
297 (traverse (car subtree
))
298 (traverse (cdr subtree
)))
299 (push subtree list
)))))