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 (defmacro doseq ((elt seq &optional index) &body body)
17 (let* ((nseq (gensym "seq"))
18 (i (or index (gensym "i")))
29 (dotimes (,i (length ,nseq))
30 (let ((,elt (aref ,nseq ,i)))
33 (defun find (item seq &key key (test #'eql))
36 (when (funcall test (funcall key x) item)
39 (when (funcall test x item)
42 (defun find-if (predicate sequence &key (key #'identity))
43 (do-sequence (x sequence)
44 (when (funcall predicate (funcall key x))
47 (defun some (function seq)
48 (do-sequence (elt seq)
49 (when (funcall function elt)
50 (return-from some t))))
52 (defun every (function seq)
53 (do-sequence (elt seq)
54 (unless (funcall function elt)
55 (return-from every nil)))
58 (defun position (elt sequence)
72 (let* ((head (cons nil nil))
76 (let ((new (list elt)))
82 (doseq (elt seq index)
84 ;; Copy the beginning of the vector only when we find an element
85 ;; that does not match.
87 (setq vector (make-array 0))
89 (vector-push-extend (aref seq i) vector)))
91 (vector-push-extend elt vector))))
95 ;;; TODO: Support vectors
97 (defun remove-if (func list)
101 ((funcall func (car list))
102 (remove-if func (cdr list)))
105 (cons (car list) (remove-if func (cdr list))))))
107 (defun remove-if-not (func list)
111 ((funcall func (car list))
112 (cons (car list) (remove-if-not func (cdr list))))
114 (remove-if-not func (cdr list)))))