4d47's solution

to Bob in the Clojure Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Bob is a lackadaisical teenager. In conversation, his responses are very limited.

He answers 'Whoa, chill out!' if you yell at him.

He answers 'Calm down, I know what I'm doing!' if you yell a question at him.

He says 'Fine. Be that way!' if you address him without actually saying anything.

He answers 'Whatever.' to anything else.

Source

Inspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=06

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

bob_test.clj

``````(ns bob-test
(:require [clojure.test :refer [deftest is]]
bob))

(deftest responds-to-something
(is (= "Whatever." (bob/response-for "Tom-ay-to, tom-aaaah-to."))))

(deftest responds-to-shouts
(is (= "Whoa, chill out!" (bob/response-for "WATCH OUT!"))))

(deftest responds-to-questions
(is (= "Sure."
(bob/response-for "Does this cryogenic chamber make me look fat?"))))

(deftest responds-to-forceful-talking
(is (= "Whatever." (bob/response-for "Let's go make out behind the gym!"))))

(deftest responds-to-acronyms
(is (= "Whatever."
(bob/response-for "It's OK if you don't want to go to the DMV."))))

(deftest responds-to-forceful-questions
(is (= "Calm down, I know what I'm doing!"
(bob/response-for "WHAT THE HELL WERE YOU THINKING?"))))

(deftest responds-to-shouting-with-special-characters
(is (= "Whoa, chill out!"
(bob/response-for "ZOMG THE %^*@#\$(*^ ZOMBIES ARE COMING!!11!!1!"))))

(deftest responds-to-shouting-numbers
(is (= "Whoa, chill out!" (bob/response-for "1, 2, 3 GO!"))))

(deftest responds-to-shouting-with-no-exclamation-mark
(is (= "Whoa, chill out!" (bob/response-for "I HATE YOU"))))

(deftest responds-to-statement-containing-question-mark
(is (= "Whatever." (bob/response-for "Ending with ? means a question."))))

(deftest responds-to-silence
(is (= "Fine. Be that way!" (bob/response-for ""))))

(deftest responds-to-prolonged-silence
(is (= "Fine. Be that way!" (bob/response-for "    "))))

(deftest responds-to-only-numbers
(is (= "Whatever." (bob/response-for "1, 2, 3"))))

(deftest responds-to-number-question
(is (= "Sure." (bob/response-for "4?"))))``````
``````(ns bob
(:require [clojure.string :as str]))

(defn- yelling? [s]
(and (= s (str/upper-case s))
(some #(Character/isLetter %) s)))

(defn- question? [s]
(re-find #"\?\s*\$" s))

(defmacro cond-pred [expr & clauses]
`(condp apply [~expr] ~@clauses))

(defn response-for [s]
(cond-pred s
str/blank? "Fine. Be that way!"
yelling? "Whoa, chill out!"
question? "Sure."
(constantly true) "Whatever."))``````