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