RNA Transcription
Turn a DNA strand into its RNA complement: G to C, C to G, T to A and A to U.
Adapted from Exercism’s problem specification (MIT). Listed under Encoding & Ciphers.
// FILE: rna_transcription.nomi
//# Transcribes a DNA strand to its RNA complement.
/// The RNA complement of a DNA strand: G to C, C to G, T to A and A to U.
/// A strand holding anything but those four nucleotides is an error.
pub fn to_rna(dna: String) -> Result<String, String>
dna
|> Iter.map(complement)
|> Result.collect()?
|> String.join()
|> Ok()
end
fn complement(nucleotide: String) -> Result<String, String>
case nucleotide
"G" -> Ok("C")
"C" -> Ok("G")
"T" -> Ok("A")
"A" -> Ok("U")
_ -> Err("invalid nucleotide '${nucleotide}'")
end
end
// FILE: rna_transcription_test.nomi
import rna_transcription.to_rna
test "transcribes DNA to RNA"
[
("Empty RNA sequence", "", ""),
("RNA complement of cytosine is guanine", "C", "G"),
("RNA complement of guanine is cytosine", "G", "C"),
("RNA complement of thymine is adenine", "T", "A"),
("RNA complement of adenine is uracil", "A", "U"),
("RNA complement", "ACGTGGTCTTAA", "UGCACCAGAAUU"),
]
|> subtests (description, dna, expected), description
assert to_rna(dna) == Ok(expected)
end
endok transcribes DNA to RNA (6 cases)
test result: ok. 6 passed, 0 failed
How it’s written
Section titled “How it’s written”to_rnareturnsResult<String, String>, though the canonical data has no error case. A character other than the four nucleotides has no complement, and the alternatives were a wrong answer or a trap. The tests compare withOk(expected).- The nucleotides stay one-letter Strings matched by a
case, not an enum. An enum would need a parse step and a render step around a four-arm table for no gain in a function this size. Result.collect()?turns the per-nucleotide results into one and passes the first invalid nucleotide up as the error; the parts are then joined withString.join()and wrapped inOk.