Extract the body of define-constant macro into a function to avoid some warnings.
[alexandria.git] / lists.lisp
blob574dcc556465f740e7373aab2dd599d9d4bbc414
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."
6 (let (plist)
7 (dolist (pair alist)
8 (push (car pair) plist)
9 (push (cdr pair) plist))
10 (nreverse 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."
15 (let (alist)
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)
29 `(block nil
30 (flet ((,results ()
31 (let (,key ,val)
32 (declare (ignorable ,key ,val))
33 (return ,values))))
34 (let* ((,tail ,plist)
35 (,key (if ,tail
36 (pop ,tail)
37 (,results)))
38 (,val (if ,tail
39 (pop ,tail)
40 (malformed-plist ',plist))))
41 (declare (ignorable ,key ,val))
42 ,@declarations
43 (tagbody
44 ,loop
45 ,@forms
46 (setf ,key (if ,tail
47 (pop ,tail)
48 (,results))
49 ,val (if ,tail
50 (pop ,tail)
51 (malformed-plist ',plist)))
52 (go ,loop))))))))
54 (define-modify-macro appendf (&rest lists) append
55 "Modify-macro for APPEND. Appends LISTS to the place designated by the first
56 argument.")
58 (define-modify-macro nconcf (&rest lists) nconc
59 "Modify-macro for NCONC. Concatenates LISTS to place designated by the first
60 argument.")
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
69 either argument.")
71 (defun circular-list (&rest elements)
72 "Creates a circular list of ELEMENTS."
73 (let ((cycle (copy-list elements)))
74 (nconc cycle cycle)))
76 (defun circular-list-p (object)
77 "Returns true if OBJECT is a circular list, NIL otherwise."
78 (and (listp object)
79 (do ((fast object (cddr fast))
80 (slow (cons (car object) (cdr object)) (cdr slow)))
81 (nil)
82 (unless (and (consp fast) (listp (cdr fast)))
83 (return nil))
84 (when (eq fast slow)
85 (return t)))))
87 (defun circular-tree-p (object)
88 "Returns true if OBJECT is a circular tree, NIL otherwise."
89 (labels ((circularp (object seen)
90 (and (consp object)
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)))
95 ((not (consp tail))
96 nil)
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."
105 (cond ((not object)
107 ((consp object)
108 (do ((fast object (cddr fast))
109 (slow (cons (car object) (cdr object)) (cdr slow)))
110 (nil)
111 (unless (and (listp fast) (consp (cdr fast)))
112 (return (and (listp fast) (not (cdr fast)))))
113 (when (eq fast slow)
114 (return nil))))
116 nil)))
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 `(satisfies proper-list-p))
124 (defun lastcar (list)
125 "Returns the last element of LIST. Signals a type-error if LIST is not a
126 proper list."
127 (do ((last list fast)
128 (fast list (cddr fast))
129 (slow (cons (car list) (cdr list)) (cdr slow)))
130 (nil)
131 (when (endp fast)
132 (return (cadr last)))
133 (when (endp (cdr fast))
134 (return (car fast)))
135 (when (eq fast slow)
136 (error 'type-error
137 :datum list
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
142 list."
143 (do ((last list fast)
144 (fast list (cddr fast))
145 (slow (cons (car list) (cdr list)) (cdr slow)))
146 (nil)
147 (when (endp fast)
148 (return (setf (cadr last) object)))
149 (when (endp (cdr fast))
150 (return (setf (car fast) object)))
151 (when (eq fast slow)
152 (error 'type-error
153 :datum list
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."
169 (if (consp thing)
170 (car thing)
171 thing))
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."
176 (if (consp cons)
177 cons
178 (cons cons nil)))
180 (defun ensure-list (list)
181 "If LIST is a list, it is returned. Otherwise returns the list designated by LIST."
182 (if (listp list)
183 list
184 (list 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: unoptimal: (sans '(:a 1 :b 2) :a) has no need to copy the
193 ;; tail.
194 (loop for cell = plist :then (cddr cell)
195 for key = (car cell)
196 while cell
197 unless (member key keys :test #'eq)
198 collect key
199 and do (assert (cdr cell) () "Not a proper plist")
200 and collect (cadr cell)))
202 (defun delete-from-plist (plist &rest keys)
203 "Just like REMOVE-FROM-PLIST, but this version may destructively modify the
204 provided plist."
205 ;; FIXME unoptimal
206 (apply 'remove-from-plist plist keys))
208 (define-modify-macro remove-from-plistf (&rest keys) remove-from-plist)
209 (define-modify-macro delete-from-plistf (&rest keys) delete-from-plist)
211 (declaim (inline sans))
212 (defun sans (plist &rest keys)
213 "Alias of REMOVE-FROM-PLIST for backward compatibility."
214 (apply #'remove-from-plist plist keys))
216 (defun mappend (function &rest lists)
217 "Applies FUNCTION to respective element(s) of each LIST, appending all the
218 all the result list to a single list. FUNCTION must return a list."
219 (loop for results in (apply #'mapcar function lists)
220 append results))
222 (defun setp (object &key (test #'eql) (key #'identity))
223 "Returns true if OBJECT is a list that denotes a set, NIL otherwise. A list
224 denotes a set if each element of the list is unique under KEY and TEST."
225 (and (listp object)
226 (let (seen)
227 (dolist (elt object t)
228 (let ((key (funcall key elt)))
229 (if (member key seen :test test)
230 (return nil)
231 (push key seen)))))))
233 (defun set-equal (list1 list2 &key (test #'eql) (key nil keyp))
234 "Returns true if every element of LIST1 matches some element of LIST2 and
235 every element of LIST2 matches some element of LIST1. Otherwise returns false."
236 (let ((keylist1 (if keyp (mapcar key list1) list1))
237 (keylist2 (if keyp (mapcar key list2) list2)))
238 (and (dolist (elt keylist1 t)
239 (or (member elt keylist2 :test test)
240 (return nil)))
241 (dolist (elt keylist2 t)
242 (or (member elt keylist1 :test test)
243 (return nil))))))
245 (defun map-product (function list &rest more-lists)
246 "Returns a list containing the results of calling FUNCTION with one argument
247 from LIST, and one from each of MORE-LISTS for each combination of arguments.
248 In other words, returns the product of LIST and MORE-LISTS using FUNCTION.
250 Example:
252 (map-product 'list '(1 2) '(3 4) '(5 6)) => ((1 3 5) (1 3 6) (1 4 5) (1 4 6)
253 (2 3 5) (2 3 6) (2 4 5) (2 4 6))
255 (labels ((%map-product (f lists)
256 (let ((more (cdr lists))
257 (one (car lists)))
258 (if (not more)
259 (mapcar f one)
260 (mappend (lambda (x)
261 (%map-product (curry f x) more))
262 one)))))
263 (%map-product (if (functionp function)
264 function
265 (fdefinition function))
266 (cons list more-lists))))
268 (defun flatten (tree)
269 "Traverses the tree in order, collecting non-null leaves into a list."
270 (let (list)
271 (labels ((traverse (subtree)
272 (when subtree
273 (if (consp subtree)
274 (progn
275 (traverse (car subtree))
276 (traverse (cdr subtree)))
277 (push subtree list)))))
278 (traverse tree))
279 (nreverse list)))