summaryrefslogtreecommitdiff
path: root/exercism/emacs-lisp/word-count/word-count.el
diff options
context:
space:
mode:
Diffstat (limited to 'exercism/emacs-lisp/word-count/word-count.el')
-rw-r--r--exercism/emacs-lisp/word-count/word-count.el28
1 files changed, 28 insertions, 0 deletions
diff --git a/exercism/emacs-lisp/word-count/word-count.el b/exercism/emacs-lisp/word-count/word-count.el
new file mode 100644
index 0000000..bd51772
--- /dev/null
+++ b/exercism/emacs-lisp/word-count/word-count.el
@@ -0,0 +1,28 @@
+;;; word-count.el --- word-count Exercise (exercism)
+
+;;; Commentary:
+
+(require 'cl-extra)
+
+;;; Code:
+(defun kj/extract-words (str)
+ (let ((pos 0)
+ (result nil))
+ (while (string-match "[[:blank:]'\"]*\\([[:alnum:]']+\\)" str pos)
+ (setq pos (match-end 1))
+ (push (match-string 1 str) result))
+ (nreverse result)))
+
+(defun word-count (str)
+ (let* ((table (make-hash-table :test 'equal))
+ (result nil)
+ (count-word (lambda (w)
+ (setq w (downcase w))
+ (let ((count (gethash w table 0)))
+ (puthash w (1+ count) table))))
+ (push-output (lambda (w c)
+ (push (cons w c) result))))
+ (cl-mapc count-word (kj/extract-words str))
+ (maphash push-output table)
+ result))
+;;; word-count.el ends here