From: Emanuel Berg <incal@dataswamp.org>
To: emacs-devel@gnu.org
Cc: Philip Kaludercic <philipk@posteo.net>
Subject: fun-names.el --- avoid function duplication
Date: Sat, 27 Jul 2024 09:18:27 +0200 [thread overview]
Message-ID: <8734nvgv5o.fsf@dataswamp.org> (raw)
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
next reply other threads:[~2024-07-27 7:18 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-27 7:18 Emanuel Berg [this message]
2024-07-27 10:55 ` fun-names.el --- avoid function duplication 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=8734nvgv5o.fsf@dataswamp.org \
--to=incal@dataswamp.org \
--cc=emacs-devel@gnu.org \
--cc=philipk@posteo.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).