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 (defun remove (x seq &key key (test #'eql testp) (test-not #'eql test-not-p))
71 (let* ((head (cons nil nil))
73 (do-sequence (elt seq)
74 (unless (satisfies-test-p x elt :key key :test test :testp testp
75 :test-not test-not :test-not-p test-not-p)
76 (let ((new (list elt)))
82 (do-sequence (elt seq index)
83 (if (satisfies-test-p x elt :key key :test test :testp testp
84 :test-not test-not :test-not-p test-not-p)
85 ;; Copy the beginning of the vector only when we find an element
86 ;; that does not match.
88 (setq vector (make-array 0))
90 (vector-push-extend (aref seq i) vector)))
92 (vector-push-extend elt vector))))
96 (defun some (function seq)
97 (do-sequence (elt seq)
98 (when (funcall function elt)
99 (return-from some t))))
101 (defun every (function seq)
102 (do-sequence (elt seq)
103 (unless (funcall function elt)
104 (return-from every nil)))
107 (defun remove-if (func seq)
109 ((listp seq) (list-remove-if func seq nil))
110 ((arrayp seq) (vector-remove-if func seq nil))
111 (t (not-seq-error seq))))
113 (defun remove-if-not (func seq)
115 ((listp seq) (list-remove-if func seq t))
116 ((arrayp seq) (vector-remove-if func seq t))
117 (t (not-seq-error seq))))
119 (defun list-remove-if (func list negate)
122 (let ((test (funcall func (car list))))
123 (if (if negate (not test) test)
124 (list-remove-if func (cdr list) negate)
125 (cons (car list) (list-remove-if func (cdr list) negate))))))
127 (defun vector-remove-if (func vector negate)
128 (let ((out-vector (make-array 0)))
129 (do-sequence (element vector i)
130 (let ((test (funcall func element)))
131 (when (if negate test (not test))
132 (vector-push-extend element out-vector))))
135 (defun subseq (seq a &optional b)
139 (let ((diff (- b a)))
143 (error "Start index must be smaller than end index"))
145 (let* ((drop-a (copy-list (nthcdr a seq)))
147 (dotimes (_ (1- diff))
148 (setq pointer (cdr pointer))
150 (error "Ending index larger than length of list")))
153 (copy-list (nthcdr a seq))))
155 (let* ((b (or b (length seq)))
157 (new (make-array size :element-type (array-element-type seq))))
161 (aset new i (aref seq j)))))
162 (t (not-seq-error seq))))