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