X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=src%2Fsequence.lisp;h=be36e994fedeef7904f48a9c61b48c6b91dcb71f;hb=25d3ce6406a74dca652ff4bb27f025986626958a;hp=789a7c3d2d432f8404fd4a4bcbb928025c61e638;hpb=243f68d9f0d3ec5456947968be12ebda45a60883;p=jscl.git diff --git a/src/sequence.lisp b/src/sequence.lisp index 789a7c3..be36e99 100644 --- a/src/sequence.lisp +++ b/src/sequence.lisp @@ -13,6 +13,8 @@ ;; You should have received a copy of the GNU General Public License ;; along with JSCL. If not, see . +(/debug "loading sequence.lisp!") + (defun not-seq-error (thing) (error "`~S' is not of type SEQUENCE" thing)) @@ -180,26 +182,28 @@ (defun copy-seq (sequence) (subseq sequence 0)) -;;; Based on the SBCL's reduce implementation + +;;; Reduce (based on SBCL's version) + (defun reduce (function sequence &key key from-end (start 0) end (initial-value nil ivp)) (let ((key (or key #'identity)) (end (or end (length sequence)))) (if (= end start) (if ivp initial-value (funcall function)) - (if from-end - (let ((sequence (nthcdr (- (length sequence) end) (reverse sequence)))) - (do ((count (if ivp start (1+ start)) - (1+ count)) - (sequence (if ivp sequence (cdr sequence)) - (cdr sequence)) - (value (if ivp initial-value (funcall key (car sequence))) - (funcall function (funcall key (car sequence)) value))) - ((>= count end) value))) - (let ((sequence (nthcdr start sequence))) - (do ((count (if ivp start (1+ start)) - (1+ count)) - (sequence (if ivp sequence (cdr sequence)) - (cdr sequence)) - (value (if ivp initial-value (funcall key (car sequence))) - (funcall function value (funcall key (car sequence))))) - ((>= count end) value))))))) + (macrolet ((reduce-list (function sequence key start end initial-value ivp from-end) + `(let ((sequence + ,(if from-end + `(reverse (nthcdr ,start ,sequence)) + `(nthcdr ,start ,sequence)))) + (do ((count (if ,ivp ,start (1+ ,start)) + (1+ count)) + (sequence (if ,ivp sequence (cdr sequence)) + (cdr sequence)) + (value (if ,ivp ,initial-value (funcall ,key (car sequence))) + ,(if from-end + `(funcall ,function (funcall ,key (car sequence)) value) + `(funcall ,function value (funcall ,key (car sequence)))))) + ((>= count ,end) value))))) + (if from-end + (reduce-list function sequence key start end initial-value ivp t) + (reduce-list function sequence key start end initial-value ivp nil))))))