(ql:quickload '(:pileup :iterate)) (use-package :iterate) (defun heap-vector (heap) (let ((result (make-array (pileup:heap-count heap))) (i 0)) (pileup:map-heap (lambda (el) (setf (aref result i) el)) heap) result)) (defun heap-list (heap) (let ((result nil)) (pileup:map-heap (lambda (el) (push el result)) heap) (nreverse result))) (defmacro-clause (SORTING expr &optional INTO var RESULT-TYPE type PREDICATE pred) (let ((type% (gensym)) (pred% (gensym)) (var (or var iterate::*result-var*)) (type (or type ''vector)) (pred (or pred ''<))) `(progn (with ,type% = ,type) (with ,pred% = ,pred) (accumulating ,expr :by (lambda (val seq) (pileup:heap-insert val seq) seq) :initial-value (pileup:make-heap ,pred%) :into ,var) (finally (setf ,var (ecase ,type% (list (heap-list ,var)) (vector (heap-vector ,var)))))))) (iterate (repeat 10) (for x = (random 100)) (collect x :result-type 'vector :into foo) (print (type-of foo)) (finally (print (type-of foo)))) ; (66 77 50 52 11 18 62 39 89 2) (iterate (repeat 10) (for x = (random 100)) (sorting x :result-type 'list :predicate #'> :into result) (finally (print result))) ; (97 89 73 73 71 66 48 48 26 1)