Sha256: 860dab9f37601fbe60b1d1bbf958429b38d78ca25b746dc15eb6839e7f2b22f0

Contents?: true

Size: 652 Bytes

Versions: 69

Compression:

Stored size: 652 Bytes

Contents

defmodule Anagram do
  @doc """
  Returns those words that are anagrams of the `w` word.

  Comparison is case insensitive.
  """
  @spec match(String.t(), [String.t()]) :: [String.t()]
  def match(target, words) do
    lc_target = String.downcase(target)
    sorted_target = sort(lc_target)

    Enum.filter(words, fn word ->
      lc_word = String.downcase(word)
      # `and` is shortcutting
      lc_word != lc_target and sort(lc_word) == sorted_target
    end)
  end

  defp sort(s) do
    # :lists.sort is specialized on lists, which saves the overhead of the
    # reduce based Enum functions.
    String.graphemes(s) |> :lists.sort()
  end
end

Version data entries

69 entries across 69 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.179 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.178 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.177 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.176 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.175 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.174 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.173 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.172 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.171 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.170 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.169 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.167 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.166 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.165 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.164 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.163 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.162 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.161 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.160 tracks/elixir/exercises/anagram/example.exs