From 0b9ffd8ca1e14a7bd36346873a48547bf78f21ef Mon Sep 17 00:00:00 2001 From: Luke Westby Date: Sat, 9 Apr 2016 19:48:05 -0700 Subject: [PATCH] refactor nucleotide-count --- .../nucleotide-count/NucleotideCount.elm | 1 + .../nucleotide-count/NucleotideCount.example | 49 +++++++++++++++---- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/exercises/nucleotide-count/NucleotideCount.elm b/exercises/nucleotide-count/NucleotideCount.elm index f046fd8..4275b05 100644 --- a/exercises/nucleotide-count/NucleotideCount.elm +++ b/exercises/nucleotide-count/NucleotideCount.elm @@ -1,5 +1,6 @@ module NucleotideCount (..) where +version : Int version = 2 diff --git a/exercises/nucleotide-count/NucleotideCount.example b/exercises/nucleotide-count/NucleotideCount.example index efdcb13..f22d566 100644 --- a/exercises/nucleotide-count/NucleotideCount.example +++ b/exercises/nucleotide-count/NucleotideCount.example @@ -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