jsonify

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

dogweather

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

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

Qqwy

TypeCheck Core Team

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.

Last Post!

NobbZ

NobbZ

It wont, as elixir has to assume that all functions are impure and might cause side effects it can not swap out similar function calls with a common value. For the same reason it also can’t swap out variables with the function call creating their value.

All it is allowed to do is to inline functions by replacing the call with the functions body, but due to technical limitations resulting from hot code reloading this is only allowed from within the same module.

Where Next?

Popular in Discussions Top

AstonJ
Are there any Elixir or Erlang libraries that help with this? I’ve been thinking how streaming services like twitch have exploded recentl...
New
acrolink
How does the two languages compare when it comes to server side application development? Any experiences or ideas? Thank you.
New
slashdotdash
Phoenix Live View is now publicly available on GitHub. Here’s Chris McCord’s tweet announcing making it public.
New
Fl4m3Ph03n1x
Background A few days ago I was listening to The future of Elixir from Elixir Talks, with Dave Thomas (@pragdave ) and Brian Mitchell. I...
New
mikl
I wanted to capitalize a string, and tried using String.capitalize(). That generally works well, until you try to capitalize a word like...
New
mmmrrr
Just saw that dhh announced https://hotwire.dev/ Is it just me or is this essentially live view? :smiley: Although I like the “iFrame-e...
New
pillaiindu
I want to convert a Phoenix LiveView CRUD website to a CRUD mobile app. What do you think is the easiest way to do so?
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement