Added a faster loop based remove-from-plist
[alexandria.git] / lists.lisp
blob1fabed3c54a65e9b34723ee043c6c73f943aab7d
1 (in-package :alexandria)
3 (define-modify-macro appendf (&rest lists) append
4 "Modify-macro for APPEND. Appends LISTS to the place designated by the first
5 argument.")
7 (defun circular-list (&rest elements)
8 "Creates a circular list of ELEMENTS."
9 (let ((cycle (copy-list elements)))
10 (nconc cycle cycle)))
12 (defun circular-list-p (object)
13 "Returns true if OBJECT is a circular list, NIL otherwise."
14 (and (listp object)
15 (do ((fast object (cddr fast))
16 (slow (cons (car object) (cdr object)) (cdr slow)))
17 (nil)
18 (unless (and (consp fast) (listp (cdr fast)))
19 (return nil))
20 (when (eq fast slow)
21 (return t)))))
23 (defun circular-tree-p (object)
24 "Returns true if OBJECT is a circular tree, NIL otherwise."
25 (labels ((circularp (object seen)
26 (and (consp object)
27 (do ((fast (cons (car object) (cdr object)) (cddr fast))
28 (slow object (cdr slow)))
29 ((or (not (consp fast)) (not (consp (cdr slow))))
30 (do ((tail object (cdr tail)))
31 ((not (consp tail))
32 nil)
33 (let ((elt (car tail)))
34 (circularp elt (cons object seen)))))
35 (when (or (eq fast slow) (member slow seen))
36 (return-from circular-tree-p t))))))
37 (circularp object nil)))
39 (defun proper-list-p (object)
40 "Returns true if OBJECT is a proper list."
41 (cond ((not object)
43 ((consp object)
44 (do ((fast object (cddr fast))
45 (slow (cons (car object) (cdr object)) (cdr slow)))
46 (nil)
47 (unless (and (listp fast) (consp (cdr fast)))
48 (return (and (listp fast) (not (cdr fast)))))
49 (when (eq fast slow)
50 (return nil))))
52 nil)))
54 (deftype proper-list ()
55 "Type designator for proper lists. Implemented as a SATISFIES type, hence
56 not recommended for performance intensive use. Main usefullness as a type
57 designator of the expected type in a TYPE-ERROR."
58 `(satisfies proper-list-p))
60 (defun lastcar (list)
61 "Returns the last element of LIST. Signals a type-error if LIST is not a
62 proper list."
63 (do ((last list fast)
64 (fast list (cddr fast))
65 (slow (cons (car list) (cdr list)) (cdr slow)))
66 (nil)
67 (when (endp fast)
68 (return (cadr last)))
69 (when (endp (cdr fast))
70 (return (car fast)))
71 (when (eq fast slow)
72 (error 'type-error
73 :datum list
74 :expected-type '(and list (not circular-list))))))
76 (defun (setf lastcar) (object list)
77 "Sets the last element of LIST. Signals a type-error if LIST is not a proper
78 list."
79 (do ((last list fast)
80 (fast list (cddr fast))
81 (slow (cons (car list) (cdr list)) (cdr slow)))
82 (nil)
83 (when (endp fast)
84 (return (setf (cadr last) object)))
85 (when (endp (cdr fast))
86 (return (setf (car fast) object)))
87 (when (eq fast slow)
88 (error 'type-error
89 :datum list
90 :expected-type '(and list (not circular-list))))))
92 (defun make-circular-list (length &key initial-element)
93 "Creates a circular list of LENGTH with the given INITIAL-ELEMENT."
94 (let ((cycle (make-list length :initial-element initial-element)))
95 (nconc cycle cycle)))
97 (deftype circular-list ()
98 "Type designator for circular lists. Implemented as a SATISFIES type, so not
99 recommended for performance intensive use. Main usefullness as the
100 expected-type designator of a TYPE-ERROR."
101 `(satisfies circular-list-p))
103 (defun ensure-list (list)
104 "If LIST is a list, it is returned. Otherwise returns the list designated by LIST."
105 (if (listp list)
106 list
107 (list list)))
109 (defun remove-from-plist (plist &rest keys)
110 "Returns a propery-list with same keys and values as PLIST, except that keys
111 in the list designated by KEYS and values corresponding to them are removed.
112 The returned property-list may share structure with the PLIST, but PLIST is
113 not destructively modified."
114 (declare (optimize (speed 3)))
115 ;; FIXME: unoptimal: (sans '(:a 1 :b 2) :a) has no need to copy the
116 ;; tail.
117 (loop for cell = plist :then (cddr cell)
118 for key = (car cell)
119 while cell
120 unless (member key keys :test #'eq)
121 collect key
122 and do (assert (cdr cell) () "Not a proper plist")
123 and collect (cadr cell)))
125 (declaim (inline sans))
126 (defun sans (plist &rest keys)
127 "Alias of REMOVE-FROM-PLIST for backward compatibility."
128 (apply #'remove-from-plist plist keys))
130 (defun mappend (function &rest lists)
131 "Applies FUNCTION to respective element(s) of each LIST, appending all the
132 all the result list to a single list. FUNCTION must return a list."
133 (loop for results in (apply #'mapcar function lists)
134 append results))
136 (defun setp (object &key (test #'eql) (key #'identity))
137 "Returns true if OBJECT is a list that denotes a set, NIL otherwise. A list
138 denotes a set if each element of the list is unique under KEY and TEST."
139 (and (listp object)
140 (let (seen)
141 (dolist (elt object t)
142 (let ((key (funcall key elt)))
143 (if (member key seen :test test)
144 (return nil)
145 (push key seen)))))))
147 (defun set-equal (list1 list2 &key (test #'eql) (key nil keyp))
148 "Returns true if every element of LIST1 matches some element of LIST2 and
149 every element of LIST2 matches some element of LIST1. Otherwise returns false."
150 (let ((keylist1 (if keyp (mapcar key list1) list1))
151 (keylist2 (if keyp (mapcar key list2) list2)))
152 (and (dolist (elt keylist1 t)
153 (or (member elt keylist2 :test test)
154 (return nil)))
155 (dolist (elt keylist2 t)
156 (or (member elt keylist1 :test test)
157 (return nil))))))
159 (defun map-product (function list &rest more-lists)
160 "Returns a list containing the results of calling FUNCTION with one argument
161 from LIST, and one from each of MORE-LISTS for each combination of arguments.
162 In other words, returns the product of LIST and MORE-LISTS using FUNCTION.
164 Example:
166 (map-product 'list '(1 2) '(3 4) '(5 6)) => ((1 3 5) (1 3 6) (1 4 5) (1 4 6)
167 (2 3 5) (2 3 6) (2 4 5) (2 4 6))
169 (labels ((%map-product (f lists)
170 (let ((more (cdr lists))
171 (one (car lists)))
172 (if (not more)
173 (mapcar f one)
174 (mappend (lambda (x)
175 (%map-product (curry f x) more))
176 one)))))
177 (%map-product (if (functionp function)
178 function
179 (fdefinition function))
180 (cons list more-lists))))
182 (defun flatten (tree)
183 "Traverses the tree in order, collecting non-null leaves into a list."
184 (let (list)
185 (labels ((traverse (subtree)
186 (when subtree
187 (if (consp subtree)
188 (progn
189 (traverse (car subtree))
190 (traverse (cdr subtree)))
191 (push subtree list)))))
192 (traverse tree))
193 (nreverse list)))