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
7 (defun circular-list (&rest elements
)
8 "Creates a circular list of ELEMENTS."
9 (let ((cycle (copy-list elements
)))
12 (defun circular-list-p (object)
13 "Returns true if OBJECT is a circular list, NIL otherwise."
15 (do ((fast object
(cddr fast
))
16 (slow (cons (car object
) (cdr object
)) (cdr slow
)))
18 (unless (and (consp fast
) (listp (cdr fast
)))
23 (defun circular-tree-p (object)
24 "Returns true if OBJECT is a circular tree, NIL otherwise."
25 (labels ((circularp (object seen
)
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
)))
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."
44 (do ((fast object
(cddr fast
))
45 (slow (cons (car object
) (cdr object
)) (cdr slow
)))
47 (unless (and (listp fast
) (consp (cdr fast
)))
48 (return (and (listp fast
) (not (cdr fast
)))))
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
))
61 "Returns the last element of LIST. Signals a type-error if LIST is not a
64 (fast list
(cddr fast
))
65 (slow (cons (car list
) (cdr list
)) (cdr slow
)))
69 (when (endp (cdr fast
))
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
80 (fast list
(cddr fast
))
81 (slow (cons (car list
) (cdr list
)) (cdr slow
)))
84 (return (setf (cadr last
) object
)))
85 (when (endp (cdr fast
))
86 (return (setf (car fast
) object
)))
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
)))
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."
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
117 (loop for cell
= plist
:then
(cddr cell
)
120 unless
(member key keys
:test
#'eq
)
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
)
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."
141 (dolist (elt object t
)
142 (let ((key (funcall key elt
)))
143 (if (member key seen
:test test
)
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
)
155 (dolist (elt keylist2 t
)
156 (or (member elt keylist1
:test test
)
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.
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
))
175 (%map-product
(curry f x
) more
))
177 (%map-product
(if (functionp 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."
185 (labels ((traverse (subtree)
189 (traverse (car subtree
))
190 (traverse (cdr subtree
)))
191 (push subtree list
)))))