refactor nucleotide-count

This commit is contained in:
Luke Westby 2016-04-09 19:48:05 -07:00
parent b013837af3
commit 0b9ffd8ca1
2 changed files with 40 additions and 10 deletions

View file

@ -1,5 +1,6 @@
module NucleotideCount (..) where
version : Int
version =
2

View file

@ -3,18 +3,47 @@ module NucleotideCount (..) where
import String
version : Int
version =
2
nucleotideCounts : String -> { a : Int, t : Int, c : Int, g : Int }
type alias NucleotideCounts =
{ a : Int
, t : Int
, c : Int
, g : Int
}
init : NucleotideCounts
init =
{ a = 0
, t = 0
, c = 0
, g = 0
}
update : Char -> NucleotideCounts -> NucleotideCounts
update char counts =
case char of
'A' ->
{ counts | a = counts.a + 1 }
'T' ->
{ counts | t = counts.t + 1 }
'C' ->
{ counts | c = counts.c + 1 }
'G' ->
{ counts | g = counts.g + 1 }
_ ->
counts
nucleotideCounts : String -> NucleotideCounts
nucleotideCounts sequence =
let
getCount n =
String.filter (\c -> c == n) sequence |> String.length
in
{ a = getCount 'A'
, t = getCount 'T'
, c = getCount 'C'
, g = getCount 'G'
}
String.foldl update init sequence