anchor.diff:    (replace seq value :start1 start :end1 end)
     value)
   
  -(defun filter/counted (pred seq &key count from-end (start) end
  -                                     (key #'identity))
  +(defun filter/counted (pred seq &rest args
  +                       &key count from-end (start 0) end
  +                            (key #'identity))
     "Helper for FILTER."
     (fbind (pred key)
       (declare (dynamic-extent #'pred #'key))
  -    (let* ((seq (nsubseq seq (or start 0) end))
  -           (seq (if from-end (reverse seq) seq))
  -           (ret '())
  -           (len 0))
  -      (block nil
  -        (map nil
  -             (lambda (item)
  -               (when (pred (key item))
  -                 (push item ret)
  -                 (incf len)
  -                 (when (zerop (decf count))
  -                   (return))))
  -             seq))
  -      (make-sequence-like seq len
  -                          :initial-contents (if from-end
  -                                                ret
  -                                                (nreverse ret))))))
  +    (cond
  +      ;; Simple cases.
  +      ((= count 0) (make-sequence-like seq 0))
  +      ((> count (length seq)) (apply #'filter pred seq :count nil args))
  +      (t (let ((ret (make-bucket seq)))
  +           (block nil
  +             (map-subseq (lambda (item)
  +                           (when (pred (key item))
  +                             (bucket-push seq item ret)
  +                             (when (zerop (decf count))
  +                               (return))))
  +                         seq start end from-end))
  +           (let ((seq2 (bucket-seq seq ret)))
  +             (if from-end (nreverse seq2) seq2)))))))
   
   (defun filter (pred seq &rest args &key count &allow-other-keys)
     "Almost, but not quite, an alias for `remove-if-not'.
  
positive.code:     (assort '(1 2 3) :key #'evenp) => ((1 3) (2))
      (partition #'evenp '(1 2 3)) => (2), (1 3)"
    (fbind ((test (compose pred key)))
      (seq-dispatch seq
        (progn
          (setf seq (nthcdr start seq))
          (let ((pass (queue))
                (fail (queue)))
            (flet ((pass/fail (item)
                     (if (test item)
                         (enq item pass)
                         (enq item fail))))
              (declare (dynamic-extent #'pass/fail))
              (if (null end)
                  (dolist (item seq)
                    (pass/fail item))
                  (loop for i from 0 below (- end start)
                        for item in seq
                        do (pass/fail item))))
            (values (qlist pass) (qlist fail))))
        ;; Vector.
        (progn
          (let* ((pass (make-array 0
                                   :element-type (array-element-type seq)
                                   :adjustable t
                                   :fill-pointer 0))
                 (fail (copy-array pass)))
            (loop for i from start below (or end (length seq))
                  for item = (aref seq i)
                  do (if (test item)
                         (vector-push-extend item pass)
                         (vector-push-extend item fail)))
            (values pass fail)))
        ;; Generic sequence.
        (values (remove-if pred seq :key key)
                (remove-if-not pred seq :key key)))))
  
  (defun partitions (preds seq &key (start 0) end (key #'identity))
    "Generalized version of PARTITION.
  
negative.code: 
       (filter #'oddp '(1 2 3 4 5) :count 2)
       => '(1 3)"
    (if count
        (apply #'filter/counted pred seq args)
        (apply #'remove-if-not pred seq args)))
  
  (assert (equal '(0 2 4 6 8) (filter #'evenp (iota 100) :count 5)))
  (assert (equalp #(0 2 4 6 8) (filter #'evenp (coerce (iota 100) 'vector) :count 5)))
  