Rewrite POSITION using SATISFIES-TEST-P and add missing export
[jscl.git] / src / sequence.lisp
1 ;;; sequence.lisp
2
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.
7 ;;
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.
12 ;;
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/>.
15
16 (defun not-seq-error (thing)
17   (error "`~S' is not of type SEQUENCE" thing))
18
19 (defmacro do-sequence ((elt seq &optional (index (gensym "i") index-p)) &body body)
20   (let ((nseq (gensym "seq")))
21     (unless (symbolp elt)
22       (error "`~S' must be a symbol." elt))
23     `(let ((,nseq ,seq))
24        (if (listp ,nseq)
25            ,(if index-p
26                 `(let ((,index -1))
27                    (dolist (,elt ,nseq)
28                      (incf ,index)
29                      ,@body))
30                 `(dolist (,elt ,nseq)
31                    ,@body))
32            (dotimes (,index (length ,nseq))
33              (let ((,elt (aref ,nseq ,index)))
34                ,@body))))))
35
36 (defun find (item seq &key key (test #'eql testp) (test-not #'eql test-not-p))
37   (do-sequence (x seq)
38     (when (satisfies-test-p item x :key key :test test :testp testp
39                             :test-not test-not :test-not-p test-not-p)
40       (return x))))
41
42 (defun find-if (predicate sequence &key key)
43   (if key
44       (do-sequence (x sequence)
45         (when (funcall predicate (funcall key x))
46           (return x)))
47       (do-sequence (x sequence)
48         (when (funcall predicate x)
49           (return x)))))
50
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 ) 
56       (return index))))
57
58 (defun remove (x seq)
59   (cond
60     ((null seq)
61      nil)
62     ((listp seq)
63      (let* ((head (cons nil nil))
64             (tail head))
65        (do-sequence (elt seq)
66          (unless (eql x elt)
67            (let ((new (list elt)))
68              (rplacd tail new)
69              (setq tail new))))
70        (cdr head)))
71     (t
72      (let (vector)
73        (do-sequence (elt seq index)
74          (if (eql x elt)
75              ;; Copy the beginning of the vector only when we find an element
76              ;; that does not match.
77              (unless vector
78                (setq vector (make-array 0))
79                (dotimes (i index)
80                  (vector-push-extend (aref seq i) vector)))
81              (when vector
82                (vector-push-extend elt vector))))
83        (or vector seq)))))
84
85
86 (defun some (function seq)
87   (do-sequence (elt seq)
88     (when (funcall function elt)
89       (return-from some t))))
90
91 (defun every (function seq)
92   (do-sequence (elt seq)
93     (unless (funcall function elt)
94       (return-from every nil)))
95   t)
96
97 (defun remove-if (func seq)
98   (cond
99     ((listp  seq) (list-remove-if   func seq nil))
100     ((arrayp seq) (vector-remove-if func seq nil))
101     (t (not-seq-error seq))))
102
103 (defun remove-if-not (func seq)
104   (cond
105     ((listp  seq) (list-remove-if   func seq t))
106     ((arrayp seq) (vector-remove-if func seq t))
107     (t (not-seq-error seq))))
108
109 (defun list-remove-if (func list negate)
110   (if (endp list)
111     ()
112     (let ((test (funcall func (car list))))
113       (if (if negate (not test) test)
114         (list-remove-if func (cdr list) negate)
115         (cons (car list) (list-remove-if func (cdr list) negate))))))
116
117 (defun vector-remove-if (func vector negate)
118   (let ((out-vector (make-array 0)))
119     (do-sequence (element vector i)
120       (let ((test (funcall func element)))
121         (when (if negate test (not test))
122           (vector-push-extend element out-vector))))
123     out-vector))
124
125 (defun subseq (seq a &optional b)
126   (cond
127     ((listp seq)
128      (if b
129        (let ((diff (- b a)))
130          (cond
131            ((zerop  diff) ()) 
132            ((minusp diff)
133             (error "Start index must be smaller than end index"))
134            (t
135             (let* ((drop-a (copy-list (nthcdr a seq)))
136                    (pointer drop-a))
137               (dotimes (_ (1- diff))
138                 (setq pointer (cdr pointer))
139                 (when (null pointer)
140                   (error "Ending index larger than length of list")))
141               (rplacd pointer ()) 
142               drop-a))))
143        (copy-list (nthcdr a seq))))
144     ((arrayp seq) 
145      (if b
146        (slice seq a b)
147        (slice seq a)))
148     (t (not-seq-error seq))))