emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

fun-names.el --- avoid function duplication


From: Emanuel Berg
Subject: fun-names.el --- avoid function duplication
Date: Sat, 27 Jul 2024 09:18:27 +0200
User-agent: Gnus/5.13 (Gnus v5.13)

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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]