vinhnglx / vinhnglx.github.io

0 stars 0 forks source link

TIL_27_June_2017 - Elixir functions to handle conditions #13

Open vinhnglx opened 7 years ago

vinhnglx commented 7 years ago
  def rotate(text, shift) do
    text |> to_charlist |> Enum.map(fn(item)-> shift_char(item, shift) end) |> to_string
  end

  # 3 different functions to handle multiple conditions( ?a..?z, ?A..?Z and special characters)
  defp shift_char(char, shift) when char in ?a..?z do
    shift_char_with_base(char, shift, ?a)
  end

  defp shift_char(char, shift) when char in ?A..?Z do
    shift_char_with_base(char, shift, ?A)
  end

  defp shift_char(char, _) do
    char
  end