Sha256: 5b649289f12124b1f7bdaba5f64f37baf0290d5d7bb02d5dddc0e1655c0203cf
Contents?: true
Size: 1.02 KB
Versions: 44
Compression:
Stored size: 1.02 KB
Contents
import DNA._ class DNA(dna: String) { def count(char: Char): OrError[Int] = for { nucleotide <- toNucleotide(char).right counts <- nucleotideCounts.right } yield counts(nucleotide) lazy val nucleotideCounts: OrError[NucleotideCounts] = { val zeroCounts: Either[String, NucleotideCounts] = Right(EmptyNucleotideCounts) dna.foldLeft(zeroCounts) { case (nucleotideCounts, char) => for { nucleotide <- toNucleotide(char).right counts <- nucleotideCounts.right } yield counts updated (nucleotide, counts.getOrElse(nucleotide, 0) + 1) } } private def toNucleotide(char: Char): OrError[Nucleotide] = if (DnaNucleotides.contains(char)) Right(char) else Left(s"invalid nucleotide '$char'") } object DNA { type Nucleotide = Char type NucleotideCounts = Map[Nucleotide,Int] type OrError[T] = Either[String, T] val DnaNucleotides: Set[Nucleotide] = Set('A', 'T', 'C', 'G') val EmptyNucleotideCounts: NucleotideCounts = DnaNucleotides map (_ -> 0) toMap }
Version data entries
44 entries across 44 versions & 1 rubygems