Function: msb--aggregate-alist

msb--aggregate-alist is a byte-compiled function defined in msb.el.gz.

Signature

(msb--aggregate-alist ALIST SAME-PREDICATE SORT-PREDICATE)

Documentation

Return ALIST as a sorted, aggregated alist.

In the result all items with the same car element (according to SAME-PREDICATE) are aggregated together. The alist is first sorted by SORT-PREDICATE.

Example:
(msb--aggregate-alist
 '((a . a1) (a . a2) (b . b1) (c . c3) (a . a4) (a . a3) (b . b3) (b . b2))
 (function string=)
 (lambda (item1 item2)
   (string< (symbol-name item1) (symbol-name item2))))
results in
((a a1 a2 a4 a3) (b b1 b3 b2) (c c3))

Source Code

;; Defined in /usr/src/emacs/lisp/msb.el.gz
(defun msb--aggregate-alist (alist same-predicate sort-predicate)
  "Return ALIST as a sorted, aggregated alist.

In the result all items with the same car element (according to
SAME-PREDICATE) are aggregated together.  The alist is first sorted by
SORT-PREDICATE.

Example:
\(msb--aggregate-alist
 \\='((a . a1) (a . a2) (b . b1) (c . c3) (a . a4) (a . a3) (b . b3) (b . b2))
 (function string=)
 (lambda (item1 item2)
   (string< (symbol-name item1) (symbol-name item2))))
results in
\((a a1 a2 a4 a3) (b b1 b3 b2) (c c3))"
  (when (not (null alist))
    (let (same
	  tmp-old-car
	  tmp-same
	  (first-time-p t)
	  old-car)
      (nconc
       (apply #'nconc
	      (mapcar
	       (lambda (item)
		 (cond
		  (first-time-p
		   (push (cdr item) same)
		   (setq first-time-p nil)
		   (setq old-car (car item))
		   nil)
		  ((funcall same-predicate (car item) old-car)
		   (push (cdr item) same)
		   nil)
		  (t
		   (setq tmp-same same
			 tmp-old-car old-car)
		   (setq same (list (cdr item))
			 old-car (car item))
		   (list (cons tmp-old-car (nreverse tmp-same))))))
	       (sort alist (lambda (item1 item2)
			     (funcall sort-predicate
                                      (car item1) (car item2))))))
       (list (cons old-car (nreverse same)))))))