exercism/elixir/scrabble-score/lib/scrabble.ex

22 lines
710 B
Elixir

defmodule Scrabble do
defp char_score(char) when char in ~w(A E I O U L N R S T), do: 1
defp char_score(char) when char in ~w(D G), do: 2
defp char_score(char) when char in ~w(B C M P), do: 3
defp char_score(char) when char in ~w(F H V W Y), do: 4
defp char_score(char) when char in ~w(K), do: 5
defp char_score(char) when char in ~w(J X), do: 8
defp char_score(char) when char in ~w(Q Z), do: 10
defp char_score(_char), do: 0
@doc """
Calculate the scrabble score for the word.
"""
@spec score(String.t()) :: non_neg_integer
def score(word) do
word
|> String.upcase()
|> String.graphemes()
|> Enum.reduce(0, fn char, score -> score + char_score(char) end)
end
end