exercism-elixir/anagram/lib/anagram.ex
2022-03-12 11:08:07 -04:00

15 lines
463 B
Elixir

defmodule Anagram do
@doc """
Returns all candidates that are anagrams of, but not equal to, 'base'.
"""
@spec match(String.t(), [String.t()]) :: [String.t()]
def match(base, candidates),
do:
Enum.filter(
candidates,
&(String.downcase(&1) != String.downcase(base) and
to_graphemes(&1) == to_graphemes(base))
)
defp to_graphemes(string), do: String.downcase(string) |> String.graphemes() |> Enum.sort()
end