There are several libraries that support collecting things generated in loops (e.g. COLLECTING, collections). Rather than collecting all of the items, this library provides a way to collect just the "best" items.

To find the three closest vertexes to a given target point, you might do this:

(defun find-three-closest (target vertex-list distance-fn)
  (with-track-best (:keep 3 :order-by-fn #'<)
    (dolist (v vertex-list)
      (track v (funcall distance-fn v target))))))

The library home page is here: This library is by Patrick Stein

Topics: accumulators