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 &key key (test #'eql testp)
52 (test-not #'eql test-not-p))
53 (do-sequence (x sequence index)
54 (when (satisfies-test-p elt x :key key :test test :testp testp
55 :test-not test-not :test-not-p test-not-p )
58 (defun remove (x seq &key key (test #'eql testp) (test-not #'eql test-not-p))
63 (let* ((head (cons nil nil))
65 (do-sequence (elt seq)
66 (unless (satisfies-test-p x elt :key key :test test :testp testp
67 :test-not test-not :test-not-p test-not-p)
68 (let ((new (list elt)))
74 (do-sequence (elt seq index)
75 (if (satisfies-test-p x elt :key key :test test :testp testp
76 :test-not test-not :test-not-p test-not-p)
77 ;; Copy the beginning of the vector only when we find an element
78 ;; that does not match.
80 (setq vector (make-array 0))
82 (vector-push-extend (aref seq i) vector)))
84 (vector-push-extend elt vector))))
88 (defun some (function seq)
89 (do-sequence (elt seq)
90 (when (funcall function elt)
91 (return-from some t))))
93 (defun every (function seq)
94 (do-sequence (elt seq)
95 (unless (funcall function elt)
96 (return-from every nil)))
99 (defun remove-if (func seq)
101 ((listp seq) (list-remove-if func seq nil))
102 ((arrayp seq) (vector-remove-if func seq nil))
103 (t (not-seq-error seq))))
105 (defun remove-if-not (func seq)
107 ((listp seq) (list-remove-if func seq t))
108 ((arrayp seq) (vector-remove-if func seq t))
109 (t (not-seq-error seq))))
111 (defun list-remove-if (func list negate)
114 (let ((test (funcall func (car list))))
115 (if (if negate (not test) test)
116 (list-remove-if func (cdr list) negate)
117 (cons (car list) (list-remove-if func (cdr list) negate))))))
119 (defun vector-remove-if (func vector negate)
120 (let ((out-vector (make-array 0)))
121 (do-sequence (element vector i)
122 (let ((test (funcall func element)))
123 (when (if negate test (not test))
124 (vector-push-extend element out-vector))))
127 (defun subseq (seq a &optional b)
131 (let ((diff (- b a)))
135 (error "Start index must be smaller than end index"))
137 (let* ((drop-a (copy-list (nthcdr a seq)))
139 (dotimes (_ (1- diff))
140 (setq pointer (cdr pointer))
142 (error "Ending index larger than length of list")))
145 (copy-list (nthcdr a seq))))
150 (t (not-seq-error seq))))