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.139 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.138 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.137 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.136 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.135 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.134 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.133 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.132 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.131 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.130 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.129 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.128 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.127 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.126 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.125 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.124 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.123 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.122 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.121 tracks/elixir/exercises/anagram/example.exs
trackler-2.2.1.120 tracks/elixir/exercises/anagram/example.exs