Sha256: 8027a10472870ceee9b20a4cc51fde1ff7d54c52a0be24098c5cc2b67e8e2f08

Contents?: true

Size: 1.28 KB

Versions: 110

Compression:

Stored size: 1.28 KB

Contents

defmodule PigLatin do
  @doc """
  Given a `phrase`, translate it a word at a time to Pig Latin.

  Words beginning with consonants should have the consonant moved to the end of
  the word, followed by "ay".

  Words beginning with vowels (aeiou) should have "ay" added to the end of the
  word.

  Some groups of letters are treated like consonants, including "ch", "qu",
  "squ", "th", "thr", and "sch".

  Some groups are treated like vowels, including "yt" and "xr".
  """
  @spec translate(phrase :: String.t()) :: String.t()
  def translate(phrase) do
    phrase
    |> String.split(" ")
    |> Enum.map_join(" ", &to_pig_latin/1)
  end

  @consonant_sounds ["ch", "sch", "qu", "squ", "thr", "th"]
  @vowel_sounds ["xr", "yt"]
  @consonants "bcdfghjklmnpqrstvwxyz" |> String.graphemes
  @vowels "aeiou" |> String.graphemes

  for sound <- @consonant_sounds do
    defp to_pig_latin(unquote(sound) <> rest), do: "#{rest}#{unquote(sound)}ay"
  end

  for sound <- @vowel_sounds do
    defp to_pig_latin(unquote(sound) <> rest), do: "#{unquote(sound)}#{rest}ay"
  end

  for sound <- @consonants do
    defp to_pig_latin(unquote(sound) <> rest), do: "#{rest}#{unquote(sound)}ay"
  end

  for sound <- @vowels do
    defp to_pig_latin(unquote(sound) <> rest), do: "#{unquote(sound)}#{rest}ay"
  end
end

Version data entries

110 entries across 110 versions & 1 rubygems

Version Path
trackler-2.2.1.4 tracks/elixir/exercises/pig-latin/example.exs
trackler-2.2.1.3 tracks/elixir/exercises/pig-latin/example.exs
trackler-2.2.1.2 tracks/elixir/exercises/pig-latin/example.exs
trackler-2.2.1.1 tracks/elixir/exercises/pig-latin/example.exs
trackler-2.2.1.0 tracks/elixir/exercises/pig-latin/example.exs
trackler-2.2.0.6 tracks/elixir/exercises/pig-latin/example.exs
trackler-2.2.0.5 tracks/elixir/exercises/pig-latin/example.exs
trackler-2.2.0.4 tracks/elixir/exercises/pig-latin/example.exs
trackler-2.2.0.3 tracks/elixir/exercises/pig-latin/example.exs
trackler-2.2.0.2 tracks/elixir/exercises/pig-latin/example.exs
trackler-2.2.0.1 tracks/elixir/exercises/pig-latin/example.exs
trackler-2.2.0.0 tracks/elixir/exercises/pig-latin/example.exs
trackler-2.1.0.55 tracks/elixir/exercises/pig-latin/example.exs
trackler-2.1.0.54 tracks/elixir/exercises/pig-latin/example.exs
trackler-2.1.0.53 tracks/elixir/exercises/pig-latin/example.exs
trackler-2.1.0.52 tracks/elixir/exercises/pig-latin/example.exs
trackler-2.1.0.51 tracks/elixir/exercises/pig-latin/example.exs
trackler-2.1.0.50 tracks/elixir/exercises/pig-latin/example.exs
trackler-2.1.0.49 tracks/elixir/exercises/pig-latin/example.exs
trackler-2.1.0.48 tracks/elixir/exercises/pig-latin/example.exs