Created
October 20, 2013 15:29
-
-
Save ArthurClune/7071102 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | |
-- Note: this code doesn't pass the given tests. Instead I've re-written these | |
-- to show what a more type-safe solution might look like. | |
-- See https://gist.github.com/ArthurClune/7060045 for the new test file | |
-- | |
-- By doing this we make several of the tests redudant as they don't even type-check | |
-- | |
module DNA | |
( | |
count, | |
nucleotideCounts, | |
Base(..), | |
DNA(..) | |
) | |
where | |
import Data.List (foldl') | |
import qualified Data.Map.Strict as M | |
import Data.Map.Strict (Map) | |
import Data.Monoid (Monoid) | |
data Base = A | C | G | T deriving (Show, Eq, Bounded, Enum, Ord) | |
newtype DNA = DNA [Base] deriving (Monoid, Show) | |
count :: Base -> DNA -> Int | |
count b (DNA d) = length $ filter (==b) d | |
nucleotideCounts :: DNA -> Map Base Int | |
nucleotideCounts (DNA d) = foldl' countBases blankMap d | |
where | |
blankMap = M.fromList $ zip [minBound .. maxBound ] [ 0, 0 .. ] | |
countBases m b = M.insertWith (+) b 1 m |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment