SETF-functions for lastcar, first-elt, and last-elt. :KEY and :TEST for starts-with...
[alexandria.git] / lists.lisp
blob0bf4c685ec1fece277458c113bb904a609b2d4db
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 expexted 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 sans (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 ;; FIXME: unoptimal: (sans '(:a 1 :b 2) :a) has no need to copy the
115 ;; tail.
116 (do ((new nil)
117 (tail plist (cddr tail)))
118 ((endp tail)
119 (nreverse new))
120 (let ((key (car tail)))
121 (unless (member key keys)
122 (setf new (list* (cadr tail) key new))))))
124 (defun mappend (function &rest lists)
125 "Applies FUNCTION to respective element(s) of each LIST, appending all the
126 all the result list to a single list. FUNCTION must return a list."
127 (loop for results in (apply #'mapcar function lists)
128 append results))
130 (defun setp (object &key (test #'eql) (key #'identity))
131 "Returns true if OBJECT is a list that denotes a set, NIL otherwise. A list
132 denotes a set if each element of the list is unique under KEY and TEST."
133 (and (listp object)
134 (let (seen)
135 (dolist (elt object t)
136 (let ((key (funcall key elt)))
137 (if (member key seen :test test)
138 (return nil)
139 (push key seen)))))))
141 (defun set-equal (list1 list2 &key (test #'eql) (key nil keyp))
142 "Returns true if every element of LIST1 matches some element of LIST2 and
143 every element of LIST2 matches some element of LIST1. Otherwise returns false."
144 (let ((keylist1 (if keyp (mapcar key list1) list1))
145 (keylist2 (if keyp (mapcar key list2) list2)))
146 (and (dolist (elt keylist1 t)
147 (or (member elt keylist2 :test test)
148 (return nil)))
149 (dolist (elt keylist2 t)
150 (or (member elt keylist1 :test test)
151 (return nil))))))
153 (defun map-product (function list &rest more-lists)
154 "Returns a list containing the results of calling FUNCTION with one argument
155 from LIST, and one from each of MORE-LISTS for each combination of arguments.
156 In other words, returns the product of LIST and MORE-LISTS using FUNCTION.
158 Example:
160 (map-product 'list '(1 2) '(3 4) '(5 6)) => ((1 3 5) (1 3 6) (1 4 5) (1 4 6)
161 (2 3 5) (2 3 6) (2 4 5) (2 4 6))
163 (labels ((%map-product (f lists)
164 (let ((more (cdr lists))
165 (one (car lists)))
166 (if (not more)
167 (mapcar f one)
168 (mappend (lambda (x)
169 (%map-product (curry f x) more))
170 one)))))
171 (%map-product (if (functionp function)
172 function
173 (fdefinition function))
174 (cons list more-lists))))
176 (defun flatten (tree)
177 "Traverses the tree in order, collecting non-null leaves into a list."
178 (let (list)
179 (labels ((traverse (subtree)
180 (when subtree
181 (if (consp subtree)
182 (progn
183 (traverse (car subtree))
184 (traverse (cdr subtree)))
185 (push subtree list)))))
186 (traverse tree))
187 (nreverse list)))