;;; 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