Skip to content
This repository has been archived by the owner on Feb 11, 2018. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Support &key from-end in POSITION-IF
  • Loading branch information
diogoalexandrefranco authored and PuercoPop committed Apr 8, 2016
1 parent da33b31 commit ec843b0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/sequence.lisp
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions tests/seq.lisp
Expand Up @@ -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)))
Expand Down

0 comments on commit ec843b0

Please sign in to comment.