unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* fun-names.el --- avoid function duplication
@ 2024-07-27  7:18 Emanuel Berg
  2024-07-27 10:55 ` Emanuel Berg
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Emanuel Berg @ 2024-07-27  7:18 UTC (permalink / raw)
  To: emacs-devel; +Cc: Philip Kaludercic

Another idea I just wrote - see the examples what it does.

I wrote it in the style Mr. Kaludercic prefers, let's see if
he can still find things to improve :)

;;; fun-names.el --- avoid function duplication -*- lexical-binding: t -*-
;;
;; Author: Emanuel Berg <incal@dataswamp.org>
;; Created: 2024-07-27
;; Keywords: matching
;; License: GPL3+
;; URL: https://dataswamp.org/~incal/emacs-init/fun-names.el
;; Version: 1.0.0
;;
;;; Commentary:
;;
;; Avoid function duplication based on function names.
;;
;; Use the function `fun-names' like below to find out what
;; other functions already use the same words in their names,
;; and to what degree.
;;
;; (fun-names #'fn--string-words)
;; -> nil
;;
;; (fun-names #'gnus-group-mark-buffer)
;; -> ((gnus-group-mark-group  0.875)
;;     (gnus-group-mark-regexp 0.75 )
;;     (gnus-group-mark-region 0.75 )
;;      ... )
;;
;;; Code:

(require 'cl-lib)

(defun fun-names (fun &optional limit)
  (unless (stringp fun)
    (setq fun (symbol-name fun)))
  (unless limit
    (setq limit 0.70))
  (let ((similar)
        (ratio))
    (obarray-map
      (lambda (e)
        (when (functionp e)
          (setq ratio (fn--same-words fun (symbol-name e)))
          (when (< limit ratio)
            (push (list e ratio) similar))))
      obarray)
    (cdr (cl-sort similar #'> :key #'cadr))))

(defun fn--string-words (str &optional no-sort keep-case)
  (unless keep-case
    (setq str (downcase str)))
  (let ((words (split-string str "[-[:space:]()]+" t "[[:punct:]]+")))
    (if no-sort
        words
      (sort words))))

(defun fn--same-words (s1 s2)
  (let* ((w1 (fn--string-words s1))
         (w2 (fn--string-words s2))
         (num (+ (length w1) (length w2)))
         (com (- num (length (cl-set-exclusive-or w1 w2 :test #'string=)))))
    (/ com num 1.0)))

(provide 'fun-names)
;;; fun-names.el ends here

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2024-08-03 22:48 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-27  7:18 fun-names.el --- avoid function duplication Emanuel Berg
2024-07-27 10:55 ` Emanuel Berg
2024-07-27 11:36 ` Philip Kaludercic
2024-07-27 12:14   ` Emanuel Berg
2024-07-27 14:41   ` Emanuel Berg
2024-07-27 18:00     ` Emanuel Berg
2024-07-30  2:51 ` Richard Stallman
2024-07-30  3:55   ` Emanuel Berg
2024-08-01  2:12     ` Emanuel Berg
2024-08-03 22:48       ` Emanuel Berg
2024-08-01  2:32     ` Richard Stallman

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).