-
Notifications
You must be signed in to change notification settings - Fork 14
/
tsequence.lisp
52 lines (39 loc) · 1.11 KB
/
tsequence.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
;;;; sequence functions
(defgeneric treverse (x)
(:documentation
"Return a new transactional sequence containing the same elements but in reverse order."))
(defmethod treverse ((x (eql nil)))
nil)
(defmethod treverse ((x tcons))
(declare (type tcons x))
(let ((copy))
(do-tlist (obj x)
(tpush obj copy))
copy))
(defgeneric tnreverse (x)
(:documentation
"Return a transactional sequence of the same elements in reverse order;
the argument is destroyed."))
(defmethod tnreverse ((x (eql nil)))
nil)
(defmethod tnreverse ((x tcons))
#-(and)
(loop
for top = x then curr
for curr = (prog1 (tcons-rest x)
(setf (tcons-rest x) nil)) then next
with next
until (tendp curr)
do
(setf next (tcons-rest curr)
(tcons-rest curr) top)
finally (return top))
;; equivalent to loop above, shorter compiled code on SBCL
#+(and)
(do ((top x curr)
(curr (prog1 (tcons-rest x) (setf (tcons-rest x) nil))
next)
(next))
((tendp curr) top)
(setf next (tcons-rest curr)
(tcons-rest curr) top)))