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 (defun not-seq-error (thing)
17 (error "`~S' is not of type SEQUENCE" thing))
19 (defmacro do-sequence ((elt seq &optional (index (gensym "i") index-p)) &body body)
20 (let ((nseq (gensym "seq")))
22 (error "`~S' must be a symbol." elt))
32 (dotimes (,index (length ,nseq))
33 (let ((,elt (aref ,nseq ,index)))
36 (defun find (item seq &key key (test #'eql testp) (test-not #'eql test-not-p))
38 (when (satisfies-test-p item x :key key :test test :testp testp
39 :test-not test-not :test-not-p test-not-p)
42 (defun find-if (predicate sequence &key key)
44 (do-sequence (x sequence)
45 (when (funcall predicate (funcall key x))
47 (do-sequence (x sequence)
48 (when (funcall predicate x)
51 (defun position (elt sequence
52 &key key (test #'eql testp)
53 (test-not #'eql test-not-p)
55 ;; TODO: Implement START and END efficiently for all the sequence
57 (let ((end (or end (length sequence))))
58 (do-sequence (x sequence index)
59 (when (and (<= start index)
61 (satisfies-test-p elt x
62 :key key :test test :testp testp
63 :test-not test-not :test-not-p test-not-p))
66 ;; TODO: need to support &key from-end
67 (defun position-if (predicate sequence
68 &key key (start 0) end)
69 ;; TODO: Implement START and END efficiently for all the sequence
71 (let ((end (or end (length sequence))))
72 (do-sequence (x sequence index)
73 (when (and (<= start index)
75 (funcall predicate (if key (funcall key x) x)))
78 (defun position-if-not (predicate sequence
79 &key key (start 0) end)
80 (position-if (complement predicate) sequence :key key :start start :end end))
82 (defun remove (x seq &key key (test #'eql testp) (test-not #'eql test-not-p))
87 (let* ((head (cons nil nil))
89 (do-sequence (elt seq)
90 (unless (satisfies-test-p x elt :key key :test test :testp testp
91 :test-not test-not :test-not-p test-not-p)
92 (let ((new (list elt)))
98 (do-sequence (elt seq index)
99 (if (satisfies-test-p x elt :key key :test test :testp testp
100 :test-not test-not :test-not-p test-not-p)
101 ;; Copy the beginning of the vector only when we find an element
102 ;; that does not match.
104 (setq vector (make-array 0))
106 (vector-push-extend (aref seq i) vector)))
108 (vector-push-extend elt vector))))
112 (defun some (function seq)
113 (do-sequence (elt seq)
114 (when (funcall function elt)
115 (return-from some t))))
117 (defun every (function seq)
118 (do-sequence (elt seq)
119 (unless (funcall function elt)
120 (return-from every nil)))
123 (defun remove-if (func seq)
125 ((listp seq) (list-remove-if func seq nil))
126 ((arrayp seq) (vector-remove-if func seq nil))
127 (t (not-seq-error seq))))
129 (defun remove-if-not (func seq)
131 ((listp seq) (list-remove-if func seq t))
132 ((arrayp seq) (vector-remove-if func seq t))
133 (t (not-seq-error seq))))
135 (defun list-remove-if (func list negate)
138 (let ((test (funcall func (car list))))
139 (if (if negate (not test) test)
140 (list-remove-if func (cdr list) negate)
141 (cons (car list) (list-remove-if func (cdr list) negate))))))
143 (defun vector-remove-if (func vector negate)
144 (let ((out-vector (make-array 0)))
145 (do-sequence (element vector i)
146 (let ((test (funcall func element)))
147 (when (if negate test (not test))
148 (vector-push-extend element out-vector))))
151 (defun subseq (seq a &optional b)
155 (let ((diff (- b a)))
159 (error "Start index must be smaller than end index"))
161 (let* ((drop-a (copy-list (nthcdr a seq)))
163 (dotimes (_ (1- diff))
164 (setq pointer (cdr pointer))
166 (error "Ending index larger than length of list")))
169 (copy-list (nthcdr a seq))))
171 (let* ((b (or b (length seq)))
173 (new (make-array size :element-type (array-element-type seq))))
177 (aset new i (aref seq j)))))
178 (t (not-seq-error seq))))
180 (defun copy-seq (sequence)
184 ;;; Reduce (based on SBCL's version)
186 (defun reduce (function sequence &key key from-end (start 0) end (initial-value nil ivp))
187 (let ((key (or key #'identity))
188 (end (or end (length sequence))))
190 (if ivp initial-value (funcall function))
191 (macrolet ((reduce-list (function sequence key start end initial-value ivp from-end)
194 `(reverse (nthcdr ,start ,sequence))
195 `(nthcdr ,start ,sequence))))
196 (do ((count (if ,ivp ,start (1+ ,start))
198 (sequence (if ,ivp sequence (cdr sequence))
200 (value (if ,ivp ,initial-value (funcall ,key (car sequence)))
202 `(funcall ,function (funcall ,key (car sequence)) value)
203 `(funcall ,function value (funcall ,key (car sequence))))))
204 ((>= count ,end) value)))))
206 (reduce-list function sequence key start end initial-value ivp t)
207 (reduce-list function sequence key start end initial-value ivp nil))))))