summaryrefslogtreecommitdiff
path: root/exercism/emacs-lisp/bob/bob.el
blob: 1482772afa3a8c11574aac34af5b87a8285bbcf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
;;; bob.el --- Bob exercise (exercism)

;;; Commentary:

;;; Code:
(require 'cl-macs)
(require 'seq)

(cl-defstruct (utterance (:constructor utterance-create)
                         (:copier nil))
  is-question
  has-uppercase
  all-uppercase
  has-content)

(defun utterance-is-yelling (utterance)
  (and (utterance-has-uppercase utterance)
       (utterance-all-uppercase utterance)))

(defun update-utterance (utterance c)
  (let ((s (string c))
        (case-fold-search nil))
    (unless (string-match-p "[[:space:]]" s)
      (setf (utterance-is-question utterance) (eql ?? c))
      (setf (utterance-has-content utterance) t)

      (when (string-match-p "[[:upper:]]" s)
        (setf (utterance-has-uppercase utterance) t))

      (when (string-match-p "[[:lower:]]" s)
        (setf (utterance-all-uppercase utterance) nil)))

    utterance))

(defun response-for (message)
  (let* ((utterance
         (seq-reduce
          'update-utterance
          message
          (utterance-create
           :is-question nil
           :has-uppercase nil
           :all-uppercase t
           :has-content nil)))
         (is-question (utterance-is-question utterance))
         (is-yelling (utterance-is-yelling utterance)))
    (cond
     ((not (utterance-has-content utterance))
      "Fine. Be that way!")
     ((and is-question is-yelling)
      "Calm down, I know what I'm doing!")
     ((and is-question (not is-yelling))
      "Sure.")
     ((and (not is-question) is-yelling)
      "Whoa, chill out!" )
     ((and (not is-question) (not is-yelling))
      "Whatever." ))))

(provide 'bob)
;;; bob.el ends here