2

I have the strong feeling that the functionality of the following two functions should already be provided by some standard macro or function in emacs. It would be nice if someone could give me a pointer to those.

(defun classify (classifier list &rest key-values)
  "Maps the LIST entries through CLASSIFIER to class denotators.
Returns the alist of equivalence classes.  Each equivalence class
is a cons whose `car' is the class denotator and the cdr is the
list of members.
Keywords supported: :test"
  (let ((test (or (plist-get key-values :test) 'equal)))
    (let (classify-res)
      (dolist (classify-li list)
    (let* ((classify-key (funcall classifier classify-li))
           (classify-class (cl-assoc classify-key classify-res :test test)))
      (if classify-class
          (setcdr classify-class (cons classify-li (cdr classify-class)))
        (setq classify-res (cons (list classify-key classify-li) classify-res)))))
      classify-res)))

(defun max-el (list &rest key-values)
  "Return maximal element.
Keywords supported: :test"
  (let ((max-el-max (car-safe list))
    (max-el-test (or (plist-get key-values :test) '>)))
    (setq list (cdr list))
    (while list
      (when (funcall max-el-test (car list) max-el-max)
    (setq max-el-max (car list)))
      (setq list (cdr list)))
    max-el-max))

Example:

(setq classes (classify 'file-name-extension
               '("first.txt" "second.txt"
                 "script1.el" "script2.el" "script3.el")))

gives

(("el" "script3.el" "script2.el" "script1.el") ("txt" "second.txt" "first.txt"))

and

(max-el classes :test  (lambda (c1 c2) (> (length c1) (length c2))))

gives

("el" "script3.el" "script2.el" "script1.el")

Note, that I did not care very much about the efficiency of the function definitions above since I assume that there is already an efficient standard version. These definitions are just listed here to show which behavior I need.

2
  • 1
    No, these are not part of Emacs. After all, Emacs Lisp is pretty minimal compared to other Lisp dialects. They are part of dash.el though, you can find them there as -max-by and -group-by.
    – wasamasa
    Commented May 14, 2015 at 17:42
  • @wasamasa Seems like an answer to me!
    – glucas
    Commented May 14, 2015 at 19:50

1 Answer 1

4

While the extent of Emacs packages may suggest otherwise, Emacs Lisp is pretty small for historical reasons. To remedy this situation, one can use packages like the built-in CL library or the external dash.el. As I cannot spot the equivalents for your proposed functions in cl-lib.el, I'm going to point you to -max-by and -group-by from dash.el instead.

1
  • Thanks for your answer (+1). I thought there would be something equivalent in the built-in cl library. Sometimes it is a matter of slight re-formulation of the problem and the equivalence is not so obvious. Maybe, there is somebody out there who knows an built-in alternative. So, I am waiting a bit to give others a chance. If nothing comes up I will mark your text as the answer. (Please, remaind me in a week or so.)
    – Tobias
    Commented May 15, 2015 at 5:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.