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
2 Likes

Your solution isn’t quite generalized.

You shouldn’t have anything aside of the cond, as well as there shouldn’t be more than 4 clauses in it, Chill out, be that way, Sure, and Whatever.

There is no reason to have any of the strings to be returned at more than one place.

Also your current version of shouting does consider "1, 2, 3" shout…

The cases you need to recognize is if the input was shout (at least one alphabetic character and all alphabetics are in uppercase), if it was a question, if it was silence or if it was none of the former.

2 Likes

Your capital_letters? function returns a string, whereas I believe you’re after a true/false?

Here was my solution which passes the tests, should you want to have a peak.

http://exercism.io/submissions/8314666634e546708d242d5f6d9a06d4

2 Likes

While this is still a work in progress, my goal was to get all of the tests to pass first, then refactor while learning more about how to better use Elixir. Thank you for the feedback.

1 Like

How would you have known to use [\p{L}] in your Regex.match?/2 function? I would NEVER have even begun to figure that one out. Great solution, though.

1 Like

I know a little regex, though, Dr. Google was helpful in this instance.

1 Like

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.

3 Likes

@jsonify, I will try to break out the process some more

Punctuation regex, yielded this article http://www.regular-expressions.info/posixbrackets.html

That was working well, except it was leaving in $ and ^, so added those on top.

Then it was failing the last test with the diaeresis on the o (ö). It was stripping it off and turning it into a binary e.g. <<107>> or whatever. Then, after about an hour of trying stuff, I tried the option for unicode u at the end of the ~r/regex/u.

Hope that was helpful.

M

2 Likes

Actually, it is possible to fix this Exercise without using regular expressions.

Here is my submission.

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.

3 Likes

Please link to your submission at exercism, then we could like it or comment.

2 Likes

Of course, how did I forget. :sweat_smile: Here is the submission on Exercism.

3 Likes

What a great solution. Very creative. Thank you for sharing that.

1 Like

Hi All,

I’m also just starting out using Elixir coming from ruby and I was totally into the piping thing and created this solution:

After I looked at all the others I was a little confused as the approach of everybody else was totally different.

Would love to get some feedback on my solution to hear if I am totally going off on a wrong sidepath…

Thanks!

I gave some comments on the exercise directly, short version: pipes are cool, but use them wise.

1 Like

Thanks Norbert,

Basically could it be correct if I assume that you actually shouldn’t be piping with these conditionals as it is actually not about transforming data.

So in this case would pattern matching this solution be a more elixirian way of approaching this?

just tryong to wrap my head around these new concepts

1 Like

It is not just an elixir thing. When working with some construct like cond you should alsways have short and concise conditions, which are easy to read and tell you not only how data has to look like, but what it actually means.

When you want to know if some property p of data is either true or false you really should give that property a proper name and make it a function suffixed by a questionmark and use it. Just consider how much better ends_in_questionmark?(input) reads compared to input |> String.replace(~r/[\s]/, "") |> String.replace(~r/[\d]/, "") |> String.last == "?", just consider how much question?(input) does even better suites the context of the exercise!

Katrina Owen (creator of exercism) gave some nice talks about refactoring in ruby, one of these talks was even about our friend bob: Katrina Owen – Overkill; 2014. There is much stuff one can transfer to elixir. Propably not the stuff about extracting into various classes, but it would make sense to extract the predicates into separate modules. Haven’t done it so far in my own solutions though, since I’ve watched Katrinas talks just 14 days ago.

2 Likes

Here is mine solution, so far http://exercism.io/submissions/5896120477d44e048c136807ba851b4d

I’m confident that you meant:

defp shout_numbers?(message) do
  message
  |> String.upcase
  |> String.ends_with?("?")
end

And, in:

You actually meant:

defp capitals_letters?(message) do
   message
   |> String.split(" ")
   |> String.capitalize
   |> Enum.join(" ")
end

If I’m mistaken or you corrected it already, I apologize.

2 Likes

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

4 Likes

Hi @dogweather. I like your ‘hey’ function but have a few thoughts. Recently I’ve been coming round to the idea that, in general, fewer functions is better. Why? Because more functions = more surface area for your code which makes understanding/maintaining it more difficult. This view has been articulated (in some form) by Linus, Carmack, and Blow which lends it some credibility. That is not to say that monolithic, ginormous functions are good, but that perhaps programmers have been underestimating the costs of abstracting into multiple functions. Your solution has 7 functions. For contrast here’s my single-function solution:

defmodule Bob do
  def hey(input) do
    cond do
      String.ends_with?(input, "?") -> "Sure."
      String.upcase(input) == input and String.downcase(input) != input -> "Whoa, chill out!"
      String.trim(input) == "" -> "Fine. Be that way!"
      true -> "Whatever."
    end
  end
end

http://exercism.io/submissions/03d9b6c8dbba45f79ac817ecd356a65a.

This might be overthinking a simple exercism exercise, but that’s the point of them. To start discussion! :stuck_out_tongue: