defmodule PigLatin do @doc """ Given a `phrase`, translate it a word at a time to Pig Latin. """ @vowels ~w(a e i o u) @spec translate(phrase :: String.t()) :: String.t() def translate(phrase) do phrase |> String.split() |> Enum.map(&String.graphemes/1) |> Enum.map(&translate_word/1) |> Enum.join(" ") end @spec translate_word(word :: [String.t()]) :: String.t() defp translate_word(["q", "u" | rest]), do: rest ++ ~w(quay) defp translate_word(["x", "r" | _rest] = word), do: ay(word) defp translate_word(["y", "t" | _rest] = word), do: ay(word) defp translate_word([hd | _rest] = word) when hd in @vowels, do: ay(word) defp translate_word([hd, x | _rest] = word) when hd in ~w(x y) and x not in @vowels, do: ay(word) defp translate_word([hd | rest]), do: translate_word(rest ++ [hd]) @spec ay(word :: [String.t()]) :: [String.t()] defp ay(list), do: list ++ ~w(ay) end