From bb2d155ef7ae73a00eba53ed7e0d1acee6c0902e Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Tue, 18 Nov 2014 16:35:19 +0000 Subject: Define sorted insertion macro. --- sx.el | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'sx.el') diff --git a/sx.el b/sx.el index 64c555c..387c6bb 100644 --- a/sx.el +++ b/sx.el @@ -27,6 +27,21 @@ ;;; Utility Functions +(defmacro sx-sorted-insert-skip-first (newelt list &optional predicate) + "Inserted NEWELT into LIST sorted by PREDICATE. +This is designed for the (site id id ...) lists. So the first car +is intentionally skipped." + `(let ((tail ,list) + (x ,newelt)) + ;; The first element is never less-than. + (while (and + ;; We're at the end. + (cdr-safe tail) + ;; We're at the right place. + (,(or predicate #'<) x (cadr tail))) + (setq tail (cdr tail))) + (setcdr tail (cons x (cdr tail))))) + (defun sx-message (format-string &rest args) "Display a message" (message "[stack] %s" (apply #'format format-string args))) -- cgit v1.2.3