3 ;; JSCL is free software: you can redistribute it and/or
4 ;; modify it under the terms of the GNU General Public License as
5 ;; published by the Free Software Foundation, either version 3 of the
6 ;; License, or (at your option) any later version.
8 ;; JSCL is distributed in the hope that it will be useful, but
9 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ;; General Public License for more details.
13 ;; You should have received a copy of the GNU General Public License
14 ;; along with JSCL. If not, see <http://www.gnu.org/licenses/>.
16 (/debug "loading sequence.lisp!")
18 (defun not-seq-error (thing)
19 (error "`~S' is not of type SEQUENCE" thing))
21 (defmacro do-sequence ((elt seq &optional (index (gensym "i") index-p)) &body body)
22 (let ((nseq (gensym "seq")))
24 (error "`~S' must be a symbol." elt))
34 (dotimes (,index (length ,nseq))
35 (let ((,elt (aref ,nseq ,index)))
38 (defun find (item seq &key key (test #'eql testp) (test-not #'eql test-not-p))
40 (when (satisfies-test-p item x :key key :test test :testp testp
41 :test-not test-not :test-not-p test-not-p)
44 (defun find-if (predicate sequence &key key)
46 (do-sequence (x sequence)
47 (when (funcall predicate (funcall key x))
49 (do-sequence (x sequence)
50 (when (funcall predicate x)
53 (defun position (elt sequence
54 &key key (test #'eql testp)
55 (test-not #'eql test-not-p)
57 ;; TODO: Implement START and END efficiently for all the sequence
59 (let ((end (or end (length sequence))))
60 (do-sequence (x sequence index)
61 (when (and (<= start index)
63 (satisfies-test-p elt x
64 :key key :test test :testp testp
65 :test-not test-not :test-not-p test-not-p))
68 ;; TODO: need to support &key from-end
69 (defun position-if (predicate sequence
70 &key key (start 0) end)
71 ;; TODO: Implement START and END efficiently for all the sequence
73 (let ((end (or end (length sequence))))
74 (do-sequence (x sequence index)
75 (when (and (<= start index)
77 (funcall predicate (if key (funcall key x) x)))
80 (defun position-if-not (predicate sequence
81 &key key (start 0) end)
82 (position-if (complement predicate) sequence :key key :start start :end end))
84 (defun remove (x seq &key key (test #'eql testp) (test-not #'eql test-not-p))
89 (let* ((head (cons nil nil))
91 (do-sequence (elt seq)
92 (unless (satisfies-test-p x elt :key key :test test :testp testp
93 :test-not test-not :test-not-p test-not-p)
94 (let ((new (list elt)))
100 (do-sequence (elt seq index)
101 (if (satisfies-test-p x elt :key key :test test :testp testp
102 :test-not test-not :test-not-p test-not-p)
103 ;; Copy the beginning of the vector only when we find an element
104 ;; that does not match.
106 (setq vector (make-array 0))
108 (vector-push-extend (aref seq i) vector)))
110 (vector-push-extend elt vector))))
114 (defun some (function seq)
115 (do-sequence (elt seq)
116 (when (funcall function elt)
117 (return-from some t))))
119 (defun every (function seq)
120 (do-sequence (elt seq)
121 (unless (funcall function elt)
122 (return-from every nil)))
125 (defun remove-if (func seq)
127 ((listp seq) (list-remove-if func seq nil))
128 ((arrayp seq) (vector-remove-if func seq nil))
129 (t (not-seq-error seq))))
131 (defun remove-if-not (func seq)
133 ((listp seq) (list-remove-if func seq t))
134 ((arrayp seq) (vector-remove-if func seq t))
135 (t (not-seq-error seq))))
137 (defun list-remove-if (func list negate)
140 (let ((test (funcall func (car list))))
141 (if (if negate (not test) test)
142 (list-remove-if func (cdr list) negate)
143 (cons (car list) (list-remove-if func (cdr list) negate))))))
145 (defun vector-remove-if (func vector negate)
146 (let ((out-vector (make-array 0)))
147 (do-sequence (element vector i)
148 (let ((test (funcall func element)))
149 (when (if negate test (not test))
150 (vector-push-extend element out-vector))))
153 (defun subseq (seq a &optional b)
157 (let ((diff (- b a)))
161 (error "Start index must be smaller than end index"))
163 (let* ((drop-a (copy-list (nthcdr a seq)))
165 (dotimes (_ (1- diff))
166 (setq pointer (cdr pointer))
168 (error "Ending index larger than length of list")))
171 (copy-list (nthcdr a seq))))
173 (let* ((b (or b (length seq)))
175 (new (make-array size :element-type (array-element-type seq))))
179 (aset new i (aref seq j)))))
180 (t (not-seq-error seq))))
182 (defun copy-seq (sequence)
186 ;;; Reduce (based on SBCL's version)
188 (defun reduce (function sequence &key key from-end (start 0) end (initial-value nil ivp))
189 (let ((key (or key #'identity))
190 (end (or end (length sequence))))
192 (if ivp initial-value (funcall function))
193 (macrolet ((reduce-list (function sequence key start end initial-value ivp from-end)
196 `(reverse (nthcdr ,start ,sequence))
197 `(nthcdr ,start ,sequence))))
198 (do ((count (if ,ivp ,start (1+ ,start))
200 (sequence (if ,ivp sequence (cdr sequence))
202 (value (if ,ivp ,initial-value (funcall ,key (car sequence)))
204 `(funcall ,function (funcall ,key (car sequence)) value)
205 `(funcall ,function value (funcall ,key (car sequence))))))
206 ((>= count ,end) value)))))
208 (reduce-list function sequence key start end initial-value ivp t)
209 (reduce-list function sequence key start end initial-value ivp nil))))))