Refactor elisp/anagram/get-char-count

Use strings-to-list instead of mapcar.

Use cl-incf and push macros over low level list manipulation.
This commit is contained in:
dwrz
2019-02-05 02:27:15 +00:00
parent ca9c5f7d51
commit 4937074205
+5 -6
View File
@@ -6,16 +6,15 @@
(provide 'anagram)
(defun get-char-count(w)
(defun get-char-count (w)
"Return an associative list of character counts for W."
(let ((char-list (mapcar (lambda (char) (string char)) w))
(let ((char-list (string-to-list w))
(char-count ()))
;; Build an associative list from the character list.
(dolist (char char-list char-count)
(if (assoc char char-count)
(setcdr (assoc char char-count)
(1+ (cdr (assoc char char-count))))
(setq char-count (cons (cons char 1) char-count))))))
(let ((entry (assoc char char-count)))
(if entry (cl-incf (cdr entry))
(push (cons char 1) char-count))))))
(defun equal-char-count(char-count comparison-char-count)
"Return t if the submitted character counts are equal, false otherwise.