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))
39 (when (funcall test (funcall key x) item)
42 (when (funcall test x item)
45 (defun find-if (predicate sequence &key key)
47 (do-sequence (x sequence)
48 (when (funcall predicate (funcall key x))
50 (do-sequence (x sequence)
51 (when (funcall predicate x)
54 (defun position (elt sequence &key (test #'eql))
55 (do-sequence (x seq index)
56 (when (funcall test elt x)
64 (let* ((head (cons nil nil))
66 (do-sequence (elt seq)
68 (let ((new (list elt)))
74 (do-sequence (elt seq index)
76 ;; Copy the beginning of the vector only when we find an element
77 ;; that does not match.
79 (setq vector (make-array 0))
81 (vector-push-extend (aref seq i) vector)))
83 (vector-push-extend elt vector))))
87 (defun some (function seq)
88 (do-sequence (elt seq)
89 (when (funcall function elt)
90 (return-from some t))))
92 (defun every (function seq)
93 (do-sequence (elt seq)
94 (unless (funcall function elt)
95 (return-from every nil)))
98 (defun remove-if (func seq)
100 ((listp seq) (list-remove-if func seq nil))
101 ((arrayp seq) (vector-remove-if func seq nil))
102 (t (not-seq-error seq))))
104 (defun remove-if-not (func seq)
106 ((listp seq) (list-remove-if func seq t))
107 ((arrayp seq) (vector-remove-if func seq t))
108 (t (not-seq-error seq))))
110 (defun list-remove-if (func list negate)
113 (let ((test (funcall func (car list))))
114 (if (if negate (not test) test)
115 (list-remove-if func (cdr list) negate)
116 (cons (car list) (list-remove-if func (cdr list) negate))))))
118 (defun vector-remove-if (func vector negate)
119 (let ((out-vector (make-array 0)))
120 (do-sequence (element vector i)
121 (let ((test (funcall func element)))
122 (when (if negate test (not test))
123 (vector-push-extend element out-vector))))
126 (defun subseq (seq a &optional b)
130 (let ((diff (- b a)))
134 (error "Start index must be smaller than end index"))
136 (let* ((drop-a (nthcdr a seq))
138 (dotimes (n (1- diff))
139 (setq pointer (cdr pointer))
141 (error "Ending index larger than length of list")))
142 (setf (cdr pointer) nil)
149 (t (not-seq-error seq))))