From ec843b02a1e90b7e11d613988625c6816f491678 Mon Sep 17 00:00:00 2001 From: Diogo Franco Date: Thu, 7 Apr 2016 21:25:14 -0500 Subject: [PATCH] Support &key from-end in POSITION-IF --- src/sequence.lisp | 11 +++++++---- tests/seq.lisp | 1 + 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/sequence.lisp b/src/sequence.lisp index 179a316e..e24098a3 100644 --- a/src/sequence.lisp +++ b/src/sequence.lisp @@ -95,17 +95,20 @@ (return)))) result)) -;; TODO: need to support &key from-end (defun position-if (predicate sequence - &key key (start 0) end) + &key from-end key (start 0) end) ;; TODO: Implement START and END efficiently for all the sequence ;; functions. - (let ((end (or end (length sequence)))) + (let ((end (or end (length sequence))) + (result nil)) (do-sequence (x sequence index) (when (and (<= start index) (< index end) (funcall predicate (if key (funcall key x) x))) - (return index))))) + (setf result index) + (unless from-end + (return)))) + result)) (defun position-if-not (predicate sequence &key key (start 0) end) diff --git a/tests/seq.lisp b/tests/seq.lisp index 155eaf49..e1bb4172 100644 --- a/tests/seq.lisp +++ b/tests/seq.lisp @@ -43,6 +43,7 @@ ;; POSITION-IF, POSITION-IF-NOT (test (= 2 (position-if #'oddp '((1) (2) (3) (4)) :start 1 :key #'car))) (test (= 4 (position-if-not #'integerp '(1 2 3 4 X)))) ;; (hyperspec example used "5.0", but we don't have a full numeric tower yet!) +(test (= 4 (position-if #'oddp '((1) (2) (3) (4) (5)) :start 1 :key #'car :from-end t))) ; REMOVE-IF (test (equal (remove-if #'zerop '(1 0 2 0 3)) '(1 2 3)))