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 expexted 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 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
117 (tail plist
(cddr tail
)))
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
)
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."
135 (dolist (elt object t
)
136 (let ((key (funcall key elt
)))
137 (if (member key seen
:test test
)
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
)
149 (dolist (elt keylist2 t
)
150 (or (member elt keylist1
:test test
)
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.
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
))
169 (%map-product
(curry f x
) more
))
171 (%map-product
(if (functionp 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."
179 (labels ((traverse (subtree)
183 (traverse (car subtree
))
184 (traverse (cdr subtree
)))
185 (push subtree list
)))))