Other Clojure solutions.
(ns bob
(:require [clojure.string :refer [blank? ends-with? trim]]))
(defn is-question [s]
(ends-with? s "?"))
(defn is-yelling [s]
(let [alphas (filter #(Character/isLetter %) s)]
(and
(not-empty alphas)
(every?
#(Character/isUpperCase %)
alphas))))
(defn response-for
[input]
(let [s (trim input)]
(cond
(blank? s) "Fine. Be that way!"
(and (is-yelling s) (is-question s)) "Calm down, I know what I'm doing!"
(is-question s) "Sure."
(is-yelling s) "Whoa, chill out!"
:else "Whatever.")))
Other Elixir solutions.
defmodule Bob do
def hey(input) do
input |> String.trim() |> highly_sophisticated_nlp
end
defp highly_sophisticated_nlp(input) do
cond do
blank?(input) ->
"Fine. Be that way!"
question?(input) && yelling?(input) ->
"Calm down, I know what I'm doing!"
question?(input) ->
"Sure."
yelling?(input) ->
"Whoa, chill out!"
true ->
"Whatever."
end
end
defp alphabetical?(s) do
String.downcase(s) != String.upcase(s)
end
defp blank?(input) do
String.trim(input) == ""
end
defp yelling?(input) do
yellable = input |> String.split() |> Enum.filter(&alphabetical?(&1))
Enum.count(yellable) > 0 && Enum.all?(yellable, &all_caps?(&1))
end
defp question?(input) do
String.ends_with?(input, "?")
end
defp all_caps?(word) do
String.upcase(word) == word
end
end
Other Elm solutions.
module Bob exposing (hey)
hey : String -> String
hey =
String.trim >> response
response : String -> String
response remark =
if remark |> String.isEmpty then
"Fine. Be that way!"
else if (remark |> isQuestion) && (remark |> isYelling) then
"Calm down, I know what I'm doing!"
else if remark |> isQuestion then
"Sure."
else if remark |> isYelling then
"Whoa, chill out!"
else
"Whatever."
isQuestion : String -> Bool
isQuestion =
String.endsWith "?"
isYelling : String -> Bool
isYelling =
extractSpeech >> anyAndAll isYell
extractSpeech =
String.words >> filterMapBoolean (String.filter Char.isAlpha)
isYell =
String.all Char.isUpper
filterMapBoolean f =
List.filterMap (nonEmpty << f)
anyAndAll =
predicates
List.any
List.all
predicates f g p x =
f p x && g p x
nonEmpty : String -> Maybe String
nonEmpty string =
if String.isEmpty string then
Nothing
else
Just string
Other Haskell solutions.
module Bob (responseFor) where
import qualified Data.Char as C
import qualified Data.List as L
responseFor :: String -> String
responseFor str
| question && yelling = "Calm down, I know what I'm doing!"
| silent = "Fine. Be that way!"
| question = "Sure."
| yelling = "Whoa, chill out!"
| otherwise = "Whatever."
where
trim = L.dropWhileEnd C.isSpace . L.dropWhile C.isSpace
sanitized = trim str
alphas = filter C.isAlpha $ sanitized
silent = null $ sanitized
question = not silent && last sanitized == '?'
speaking = not silent && not (null alphas)
yelling = speaking && all C.isUpper alphas
Other Roc solutions.
module [response]
response : Str -> Str
response = \heyBob ->
sanitized = Str.trim heyBob
isQuestion = Str.endsWith sanitized "?"
isSilence = sanitized == ""
isYelling = strIsYelling sanitized
if isSilence then
"Fine. Be that way!"
else if isQuestion && isYelling then
"Calm down, I know what I'm doing!"
else if isQuestion then
"Sure."
else if isYelling then
"Whoa, chill out!"
else
"Whatever."
strIsYelling = \str ->
alphas = List.keepIf (Str.toUtf8 str) isAlpha
List.len alphas > 0 && List.all alphas isCapitalLetter
isAlpha : U8 -> Bool
isAlpha = \byte ->
(byte >= 'a' && byte <= 'z') || isCapitalLetter byte
isCapitalLetter : U8 -> Bool
isCapitalLetter = \byte ->
byte >= 'A' && byte <= 'Z'