Skip to content

nucleotide-count: simplify to only nucleotideCounts #482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
module DNA (count, nucleotideCounts) where
module DNA (nucleotideCounts) where

import Data.Map.Strict (Map, (!), fromDistinctAscList, fromListWith, findWithDefault)

count :: Char -> String -> Either String Int
count x xs = (!) <$> nucleotideCounts xs <*> valid x
import Data.Map.Strict (Map, fromDistinctAscList, fromListWith, findWithDefault)

nucleotideCounts :: String -> Either String (Map Char Int)
nucleotideCounts xs = fromDistinctAscList <$> mapM count' "ACGT"
Expand Down
5 changes: 1 addition & 4 deletions exercises/nucleotide-count/src/DNA.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
module DNA (count, nucleotideCounts) where
module DNA (nucleotideCounts) where

import Data.Map (Map)

count :: Char -> String -> Either String Int
count = error "You need to implement this function."

nucleotideCounts :: String -> Either String (Map Char Int)
nucleotideCounts = error "You need to implement this function."
25 changes: 3 additions & 22 deletions exercises/nucleotide-count/test/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,18 @@ import Data.Map (fromList)
import Test.Hspec (Spec, describe, it, shouldBe, shouldSatisfy)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)

import DNA (count, nucleotideCounts)
import DNA (nucleotideCounts)

main :: IO ()
main = hspecWith defaultConfig {configFastFail = True} specs

specs :: Spec
specs = describe "nucleotide-count" $ do

-- As of 2016-07-27, there was no reference file
-- for the test cases in `exercism/x-common`.
-- Test cases adapted from `exercism/x-common/triangle.json` on 2017-01-31.

let x `matches` y = x `shouldBe` Right y
let x `matchesMap` y = x `shouldBe` (Right . fromList) y

describe "count" $ do

it "empty dna strand has no adenosine" $
count 'A' "" `matches` 0

it "repetitive cytidine gets counted" $
count 'C' "CCCCC" `matches` 5

it "counts only thymidine" $
count 'T' "GGGGGTAACCCGG" `matches` 1

it "validates nucleotides" $
count 'X' "GACT" `shouldSatisfy` isLeft

it "validates strand" $
count 'G' "GACYT" `shouldSatisfy` isLeft

describe "nucleotideCounts" $ do

it "empty dna strand has no nucleotides" $
Expand All @@ -58,4 +39,4 @@ specs = describe "nucleotide-count" $ do
, ('T', 21) ]

it "validates strand" $
nucleotideCounts "GPAC" `shouldSatisfy` isLeft
nucleotideCounts "AGXXACT" `shouldSatisfy` isLeft