Sha256: 4e90cc72e7455331492666ed85c5b5a44b789187e9e64e60fa185933f18622d9

Contents?: true

Size: 1.5 KB

Versions: 63

Compression:

Stored size: 1.5 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.trim(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

63 entries across 63 versions & 1 rubygems

Version Path
trackler-2.2.1.70 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.69 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.68 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.67 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.66 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.65 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.64 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.63 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.62 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.61 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.60 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.59 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.58 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.57 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.56 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.55 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.54 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.53 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.52 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.51 tracks/elixir/exercises/bob/example.exs