commit 49370742057955483d315f70ce4a055c5bc234c8
parent ca9c5f7d5104a49d646dc6dc4c198d23eac979ee
Author: dwrz <dwrz@dwrz.net>
Date: Tue, 5 Feb 2019 02:27:15 +0000
Refactor elisp/anagram/get-char-count
Use strings-to-list instead of mapcar.
Use cl-incf and push macros over low level list manipulation.
Diffstat:
1 file changed, 5 insertions(+), 6 deletions(-)
diff --git a/elisp/anagram/anagram.el b/elisp/anagram/anagram.el
@@ -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.