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.110 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.109 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.108 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.107 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.106 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.105 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.104 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.103 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.102 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.101 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.100 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.99 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.98 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.97 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.96 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.95 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.94 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.93 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.92 tracks/elixir/exercises/bob/example.exs
trackler-2.2.1.91 tracks/elixir/exercises/bob/example.exs