Sha256: 11fb982886ab5b5c1e2c0bcabdf56ad8cf3751de47ae58f3be03944ce4b2718e

Contents?: true

Size: 1.52 KB

Versions: 265

Compression:

Stored size: 1.52 KB

Contents

defmodule Bob do
  @doc """
  Answers to `hey` like a teenager.

  ## Examples

  iex> Bob.hey("")
  "Fine. Be that way!"

  iex> Bob.hey("Do you like math?")
  "Sure."

  iex> Bob.hey("HELLO!")
  "Whoa, chill out!"

  iex> Bob.hey("Coding is cool.")
  "Whatever."
  """

  def hey(input) do
    cond do
      silent?(input)   -> "Fine. Be that way!"
      question?(input) -> "Sure."
      shouting?(input) -> "Whoa, chill out!"
      true             -> "Whatever."
    end
  end

  defp silent?(input),   do: "" == String.strip(input)
  defp shouting?(input), do: input == String.upcase(input) && letters?(input)
  defp question?(input), do: String.ends_with?(input, "?")
  defp letters?(input),  do: Regex.match?(~r/\p{L}+/, input)
end

# Another approach which abstracts knowing about string categories
# away from Bob and into a single responsibility module.
# (This has been commented out to avoid raising a needless "redefinition"
# warning)

# defmodule Message do
#   def silent?(input),   do: "" == String.strip(input)
#   def shouting?(input), do: input == String.upcase(input) && letters?(input)
#   def question?(input), do: String.ends_with?(input, "?")
#   defp letters?(input), do: Regex.match?(~r/\p{L}+/, input)
# end
#
# defmodule Bob do
#   import Message, only: [silent?: 1, shouting?: 1, question?: 1]
#
#   def hey(input) do
#     cond do
#       silent?(input)   -> "Fine. Be that way!"
#       shouting?(input) -> "Whoa, chill out!"
#       question?(input) -> "Sure."
#       true             -> "Whatever."
#     end
#   end
# end

Version data entries

265 entries across 265 versions & 1 rubygems

Version Path
trackler-2.2.1.47 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.46 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.45 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.44 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.43 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.42 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.41 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.40 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.39 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.38 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.37 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.36 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.35 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.34 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.33 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.32 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.31 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.30 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.29 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.28 tracks/elixir/exercises/bob/example.exs