jsonify
Exercism: Bob
I’m trying to continue to wrap my brain around how I should be structuring things and I’m getting stuck on the Bob exercise of Exercism. Here is my code and right now, I’m just not understanding why my “Talking in capitals” test isn’t passing like I think it should:
defmodule Bob do
def hey(" "), do: "Fine. Be that way!"
def hey(""), do: "Fine. Be that way!"
def hey(message) do
cond do
shouting?(message) -> "Whoa, chill out!"
asking?(message) -> "Sure."
shout_numbers?(message) -> "Whoa, chill out!"
capitals_letters?(message) -> "Whatever"
# true -> "Whatever."
end
end
defp shouting?(message) do
message == String.upcase(message)
end
defp asking?(message) do
String.ends_with?(message, "?")
end
defp shout_numbers?(message) do
message
|> String.upcase()
String.ends_with?(message, "?")
end
defp capitals_letters?(message) do
message
|> String.split(" ")
|> String.capitalize
Enum.join(" ")
end
end
Most Liked Responses
dogweather
I believe that the top-level function (the cond) should be a restatement of the problem specification. It should be written using terms in the problem domain (teenagers) not the solution domain (whitespace, upper case, etc.). E.g.:
def hey(input) do
cond do
question?(input) -> "Sure."
yell?(input) -> "Whoa, chill out!"
nothing?(input) -> "Fine. Be that way!"
true -> "Whatever."
end
end
See how that code pretty much reads like the assignment for the problem. This is a big advantage of functional programming.
The implementation of these should then be done at gradually lower levels of abstraction. Here’s my solution: http://exercism.io/submissions/e7f0b1696ed14eaa99bc72bf882f2459
jsonify
So if I may ask, what exactly did you Google as your search? Did you know that you needed to search for the Unicode solution? Just trying to get a better feel for how one would go about how to figure out how to come to a solution. When I saw your solution, all the pieces made sense and I thought, "of course that makes sense how that was solved. " But it’s the how that conclusion was reached that truly interests me. What steps did you go through to get the results you were looking for.
Qqwy
Actually, it is possible to fix this Exercise without using regular expressions.
That’s right. all_letters_uppercase?/1 checks if upcasing would not change the string, while downcasing would.
- First statement: True if string doesn’t contain any lowercase characters.
- Second statement: True if string contains any uppercase characters.
The logical AND of these statements will return false if either there is a lowercase character, or there are no uppercase-characters at all.








