exercism/elixir/nucleotide-count/nucleotide_count.exs

33 lines
664 B
Elixir
Raw Normal View History

2018-03-02 22:27:55 +00:00
defmodule NucleotideCount do
@nucleotides [?A, ?C, ?G, ?T]
@doc """
Counts individual nucleotides in a NucleotideCount strand.
## Examples
iex> NucleotideCount.count('AATAA', ?A)
4
iex> NucleotideCount.count('AATAA', ?T)
1
"""
@spec count([char], 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([char]) :: map
def histogram(strand) do
Map.new(@nucleotides, &{&1, count(strand, &1)})
end
end