Skip to content

Nucleotide Count

Count how often each of the four nucleotides occurs in a DNA strand, rejecting any other letter.

Adapted from Exercism’s problem specification (MIT). Listed under Strings & Text.

// FILE: nucleotide_count.nomi
//# Counts each nucleotide in a DNA strand.

/// One of the four nucleotides in a DNA strand.
pub enum Nucleotide
    A
    C
    G
    T
end

impl Nucleotide
    /// The nucleotide a one-letter code names.
    pub fn parse(letter: String) -> Result<Nucleotide, String>
        case letter
            "A" -> Ok(.A)
            "C" -> Ok(.C)
            "G" -> Ok(.G)
            "T" -> Ok(.T)
            _   -> Err("Invalid nucleotide in strand")
        end
    end
end

derive Variants for Nucleotide

/// How many times each nucleotide occurs in `strand`, with every nucleotide
/// present, or an error when the strand holds anything else.
pub fn counts(strand: String) -> Result<Map<Nucleotide, Int>, String>
    nucleotides =
        strand
        |> Iter.map(Nucleotide.parse)
        |> Result.collect()?

    none =
        Nucleotide.variants()
        |> Iter.map(&(&1, 0))
        |> Iter.to_map()

    Ok({..none, ..Iter.frequencies(nucleotides)})
end

// FILE: nucleotide_count_test.nomi
import nucleotide_count.{self, Nucleotide}

test "nucleotide counts"
    [
        ("empty strand", "", Ok({Nucleotide.A => 0, .C => 0, .G => 0, .T => 0})),
        (
            "can count one nucleotide in single-character input",
            "G",
            Ok({.A => 0, .C => 0, .G => 1, .T => 0}),
        ),
        (
            "strand with repeated nucleotide",
            "GGGGGGG",
            Ok({.A => 0, .C => 0, .G => 7, .T => 0}),
        ),
        (
            "strand with multiple nucleotides",
            "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC",
            Ok({.A => 20, .C => 12, .G => 17, .T => 21}),
        ),
        ("strand with invalid nucleotides", "AGXXACT", Err("Invalid nucleotide in strand")),
    ]
    |> subtests (description, strand, expected), description
        assert nucleotide_count.counts(strand) == expected
    end
end
  • The nucleotides are an enum, Nucleotide, with derive Variants so the zero counts come from Nucleotide.variants() rather than a hand-written four-entry map.
  • The result is Result<Map<Nucleotide, Int>, String>; the error is the canonical message as a String, since there is only one way to fail.
  • Parsing and counting are separate steps: Iter.map(Nucleotide.parse) then Result.collect()?, then Iter.frequencies, spread over the zero map: {..none, ..Iter.frequencies(nucleotides)}.
  • The five cases are one subtests table named by the description column, since each checks counts(strand) == expected; the error case is a row whose expected value is an Err.
  • Only the first row’s map qualifies its first key, {Nucleotide.A => 0, .C => 0, .G => 0, .T => 0}, and every key after it is bare: nothing outside the subtests table gives the rows their type, and that first key sets the enum for the keys after it and for the rows below it.
  • The zero map pairs each nucleotide with 0 through Iter.map(&(&1, 0)): a capture of the tuple literal is the function that builds it, which says the pairing without naming the nucleotide in a lambda.