Files
exercism/elixir/nucleotide-count/lib/nucleotide_count.ex

33 lines
729 B
Elixir

defmodule NucleotideCount do
@doc """
Counts individual nucleotides in a DNA strand.
## Examples
iex> NucleotideCount.count('AATAA', ?A)
4
iex> NucleotideCount.count('AATAA', ?T)
1
"""
@spec count(charlist(), char()) :: non_neg_integer()
def count(strand, nucleotide) do
Enum.count(strand, &(&1 == nucleotide))
end
@doc """
Returns a summary of counts by nucleotide.
## Examples
iex> NucleotideCount.histogram('AATAA')
%{?A => 4, ?T => 1, ?C => 0, ?G => 0}
"""
@spec histogram(charlist()) :: map()
def histogram(strand) do
Enum.reduce(strand, %{?A => 0, ?T => 0, ?C => 0, ?G => 0}, fn nucleotide, counts ->
Map.update(counts, nucleotide, 1, &(&1 + 1))
end)
end
end