Compiler-macro for OF-TYPE
[alexandria.git] / lists.lisp
blob35565c98bc17eaad58eee2d4580774c65b6de160
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 (define-modify-macro appendf (&rest lists) append
21 "Modify-macro for APPEND. Appends LISTS to the place designated by the first
22 argument.")
24 (define-modify-macro unionf (list) union
25 "Modify-macro for UNION. Saves the union of LIST and the contents of the
26 place designated by the first argument to the designated place.")
28 (define-modify-macro nunionf (list) nunion
29 "Modify-macro for NUNION. Saves the union of LIST and the contents of the
30 place designated by the first argument to the designated place. May modify
31 either argument.")
33 (defun circular-list (&rest elements)
34 "Creates a circular list of ELEMENTS."
35 (let ((cycle (copy-list elements)))
36 (nconc cycle cycle)))
38 (defun circular-list-p (object)
39 "Returns true if OBJECT is a circular list, NIL otherwise."
40 (and (listp object)
41 (do ((fast object (cddr fast))
42 (slow (cons (car object) (cdr object)) (cdr slow)))
43 (nil)
44 (unless (and (consp fast) (listp (cdr fast)))
45 (return nil))
46 (when (eq fast slow)
47 (return t)))))
49 (defun circular-tree-p (object)
50 "Returns true if OBJECT is a circular tree, NIL otherwise."
51 (labels ((circularp (object seen)
52 (and (consp object)
53 (do ((fast (cons (car object) (cdr object)) (cddr fast))
54 (slow object (cdr slow)))
55 ((or (not (consp fast)) (not (consp (cdr slow))))
56 (do ((tail object (cdr tail)))
57 ((not (consp tail))
58 nil)
59 (let ((elt (car tail)))
60 (circularp elt (cons object seen)))))
61 (when (or (eq fast slow) (member slow seen))
62 (return-from circular-tree-p t))))))
63 (circularp object nil)))
65 (defun proper-list-p (object)
66 "Returns true if OBJECT is a proper list."
67 (cond ((not object)
69 ((consp object)
70 (do ((fast object (cddr fast))
71 (slow (cons (car object) (cdr object)) (cdr slow)))
72 (nil)
73 (unless (and (listp fast) (consp (cdr fast)))
74 (return (and (listp fast) (not (cdr fast)))))
75 (when (eq fast slow)
76 (return nil))))
78 nil)))
80 (deftype proper-list ()
81 "Type designator for proper lists. Implemented as a SATISFIES type, hence
82 not recommended for performance intensive use. Main usefullness as a type
83 designator of the expected type in a TYPE-ERROR."
84 `(satisfies proper-list-p))
86 (defun lastcar (list)
87 "Returns the last element of LIST. Signals a type-error if LIST is not a
88 proper list."
89 (do ((last list fast)
90 (fast list (cddr fast))
91 (slow (cons (car list) (cdr list)) (cdr slow)))
92 (nil)
93 (when (endp fast)
94 (return (cadr last)))
95 (when (endp (cdr fast))
96 (return (car fast)))
97 (when (eq fast slow)
98 (error 'type-error
99 :datum list
100 :expected-type '(and list (not circular-list))))))
102 (defun (setf lastcar) (object list)
103 "Sets the last element of LIST. Signals a type-error if LIST is not a proper
104 list."
105 (do ((last list fast)
106 (fast list (cddr fast))
107 (slow (cons (car list) (cdr list)) (cdr slow)))
108 (nil)
109 (when (endp fast)
110 (return (setf (cadr last) object)))
111 (when (endp (cdr fast))
112 (return (setf (car fast) object)))
113 (when (eq fast slow)
114 (error 'type-error
115 :datum list
116 :expected-type '(and list (not circular-list))))))
118 (defun make-circular-list (length &key initial-element)
119 "Creates a circular list of LENGTH with the given INITIAL-ELEMENT."
120 (let ((cycle (make-list length :initial-element initial-element)))
121 (nconc cycle cycle)))
123 (deftype circular-list ()
124 "Type designator for circular lists. Implemented as a SATISFIES type, so not
125 recommended for performance intensive use. Main usefullness as the
126 expected-type designator of a TYPE-ERROR."
127 `(satisfies circular-list-p))
129 (defun ensure-car (thing)
130 "If THING is a CONS, its CAR is returned. Otherwise THING is returned."
131 (if (consp thing)
132 (car thing)
133 thing))
135 (defun ensure-cons (cons)
136 "If CONS is a cons, it is returned. Otherwise returns a fresh cons with CONS
137 in the car, and NIL in the cdr."
138 (if (consp cons)
139 cons
140 (cons cons nil)))
142 (defun ensure-list (list)
143 "If LIST is a list, it is returned. Otherwise returns the list designated by LIST."
144 (if (listp list)
145 list
146 (list list)))
148 (defun remove-from-plist (plist &rest keys)
149 "Returns a propery-list with same keys and values as PLIST, except that keys
150 in the list designated by KEYS and values corresponding to them are removed.
151 The returned property-list may share structure with the PLIST, but PLIST is
152 not destructively modified. Keys are compared using EQ."
153 (declare (optimize (speed 3)))
154 ;; FIXME: unoptimal: (sans '(:a 1 :b 2) :a) has no need to copy the
155 ;; tail.
156 (loop for cell = plist :then (cddr cell)
157 for key = (car cell)
158 while cell
159 unless (member key keys :test #'eq)
160 collect key
161 and do (assert (cdr cell) () "Not a proper plist")
162 and collect (cadr cell)))
164 (defun delete-from-plist (plist &rest keys)
165 "Just like REMOVE-FROM-PLIST, but this version may destructively modify the
166 provided plist."
167 ;; FIXME unoptimal
168 (apply 'remove-from-plist plist keys))
170 (define-modify-macro remove-from-plistf (plist &rest keys) remove-from-plist)
171 (define-modify-macro delete-from-plistf (plist &rest keys) delete-from-plist)
173 (declaim (inline sans))
174 (defun sans (plist &rest keys)
175 "Alias of REMOVE-FROM-PLIST for backward compatibility."
176 (apply #'remove-from-plist plist keys))
178 (defun mappend (function &rest lists)
179 "Applies FUNCTION to respective element(s) of each LIST, appending all the
180 all the result list to a single list. FUNCTION must return a list."
181 (loop for results in (apply #'mapcar function lists)
182 append results))
184 (defun setp (object &key (test #'eql) (key #'identity))
185 "Returns true if OBJECT is a list that denotes a set, NIL otherwise. A list
186 denotes a set if each element of the list is unique under KEY and TEST."
187 (and (listp object)
188 (let (seen)
189 (dolist (elt object t)
190 (let ((key (funcall key elt)))
191 (if (member key seen :test test)
192 (return nil)
193 (push key seen)))))))
195 (defun set-equal (list1 list2 &key (test #'eql) (key nil keyp))
196 "Returns true if every element of LIST1 matches some element of LIST2 and
197 every element of LIST2 matches some element of LIST1. Otherwise returns false."
198 (let ((keylist1 (if keyp (mapcar key list1) list1))
199 (keylist2 (if keyp (mapcar key list2) list2)))
200 (and (dolist (elt keylist1 t)
201 (or (member elt keylist2 :test test)
202 (return nil)))
203 (dolist (elt keylist2 t)
204 (or (member elt keylist1 :test test)
205 (return nil))))))
207 (defun map-product (function list &rest more-lists)
208 "Returns a list containing the results of calling FUNCTION with one argument
209 from LIST, and one from each of MORE-LISTS for each combination of arguments.
210 In other words, returns the product of LIST and MORE-LISTS using FUNCTION.
212 Example:
214 (map-product 'list '(1 2) '(3 4) '(5 6)) => ((1 3 5) (1 3 6) (1 4 5) (1 4 6)
215 (2 3 5) (2 3 6) (2 4 5) (2 4 6))
217 (labels ((%map-product (f lists)
218 (let ((more (cdr lists))
219 (one (car lists)))
220 (if (not more)
221 (mapcar f one)
222 (mappend (lambda (x)
223 (%map-product (curry f x) more))
224 one)))))
225 (%map-product (if (functionp function)
226 function
227 (fdefinition function))
228 (cons list more-lists))))
230 (defun flatten (tree)
231 "Traverses the tree in order, collecting non-null leaves into a list."
232 (let (list)
233 (labels ((traverse (subtree)
234 (when subtree
235 (if (consp subtree)
236 (progn
237 (traverse (car subtree))
238 (traverse (cdr subtree)))
239 (push subtree list)))))
240 (traverse tree))
241 (nreverse list)))