Permalink
Browse files

refactor stats and start with datestats

  • Loading branch information...
1 parent bf5bf0f commit d02ab05314650eaa81050f480b2b3c8fbf55d2e9 @thomaschrstnsn committed Jun 19, 2011
Showing with 68 additions and 36 deletions.
  1. +14 −1 daytum.hs
  2. +10 −35 main.hs
  3. +44 −0 stats.hs
View
@@ -1,8 +1,11 @@
{-# LANGUAGE ScopedTypeVariables #-}
module Daytum
(
- DaytumRecord(..),
Activity,
+ DaytumRecord(..),
+ DaytumField,
+ uniqueFields,
+ filterByField,
daytumsFromCsvFile,
daytumFromCsvLine
)
@@ -12,6 +15,7 @@ import Data.DateTime
import System.Locale
import Safe
import CSV (parseCSV)
+import qualified Data.List as DL
type Activity = String
data DaytumRecord = Daytum { name :: String
@@ -20,6 +24,15 @@ data DaytumRecord = Daytum { name :: String
, activities :: [Activity]
} deriving (Eq, Show)
+type DaytumField a = DaytumRecord -> a
+
+uniqueFields :: Eq a => DaytumField a -> [DaytumRecord] -> [a]
+uniqueFields f xs = DL.nub $ map f xs
+
+filterByField :: Eq a => DaytumField a -> a -> [DaytumRecord] -> [DaytumRecord]
+filterByField f value xs = filter decider xs
+ where decider record = value == f record
+
-- | Parses a csv file as a list of daytum records
daytumsFromCsvFile :: String -> IO [DaytumRecord]
daytumsFromCsvFile fname = do
View
45 main.hs
@@ -1,10 +1,9 @@
module Main
where
-import Prelude hiding (minimum, maximum)
import System.Environment (getArgs)
-import qualified Data.List as DL
import Daytum
+import Daytum.Stats
main = do
~[fname] <- getArgs
@@ -13,41 +12,17 @@ main = do
putStrLn $ show $ uniqueFields name records
putStrLn $ "showing entries with name: Spinning"
let spinning = filterByField name "Spinning" records
- putStrLn $ show $ doStats date spinning
+ putStrLn $ show $ fieldStats date spinning
- let running = filterByField name "Running" records ++ filterByField name "Running (No Activity)" records
+ let actRunning = "Running"
+ let actRunningNoAct = "Running (No Activity)"
+ let pureRunning = filterByField name actRunning records
+ let nonPureRunning = filterByField name actRunningNoAct records
+ let running = pureRunning ++ nonPureRunning
putStrLn $ "showing entries with name: Running and friends"
putStrLn $ show $ amountStats running
+ putStrLn $ show $ dateStats running
+ putStrLn $ "Non-pure running records:"
+ putStrLn $ show $ length nonPureRunning
-type DaytumField a = DaytumRecord -> a
-uniqueFields :: Eq a => DaytumField a -> [DaytumRecord] -> [a]
-uniqueFields f xs = DL.nub $ map f xs
-
-filterByField :: Eq a => DaytumField a -> a -> [DaytumRecord] -> [DaytumRecord]
-filterByField f value xs = filter decider xs
- where decider record = value == f record
-
-data DaytumStats a = DaytumStats { minimum :: a,
- maximum :: a,
- count :: Int
- } deriving Show
-
-doStats :: Ord a => DaytumField a -> [DaytumRecord] -> DaytumStats a
-doStats f xs = DaytumStats { minimum = DL.minimum xs', maximum = DL.maximum xs', count = length xs' }
- where
- xs' = map f xs
-
-data DaytumAmountStats a = DaytumAmountStats { least :: a,
- most :: a,
- average :: a
- } deriving Show
-
-amountStats :: [DaytumRecord] -> DaytumAmountStats Double
-amountStats xs = DaytumAmountStats { least = minimum stats, most = maximum stats, average = avg }
- where
- stats = doStats amount xs
- xs' = map amount xs
- avg = sum xs' / fromIntegral (length xs')
-
--- data DaytumDateStats = DaytumDateStats { first :: DateTime
View
@@ -0,0 +1,44 @@
+{-# LANGUAGE DisambiguateRecordFields #-}
+module Daytum.Stats
+ (
+ DaytumFieldStats, fieldStats,
+ DaytumAmountStats, amountStats,
+ DaytumDateStats, dateStats
+ )
+where
+
+import Prelude hiding (minimum, maximum)
+import Daytum
+import qualified Data.List as DL
+import Data.DateTime
+
+data DaytumFieldStats a = DaytumFieldStats { minimum :: a,
+ maximum :: a,
+ count :: Int
+ } deriving Show
+
+fieldStats :: Ord a => DaytumField a -> [DaytumRecord] -> DaytumFieldStats a
+fieldStats f xs = DaytumFieldStats { minimum = DL.minimum xs', maximum = DL.maximum xs', count = length xs' }
+ where
+ xs' = map f xs
+
+data DaytumAmountStats a = DaytumAmountStats { least :: a,
+ most :: a,
+ average :: a
+ } deriving Show
+
+amountStats :: [DaytumRecord] -> DaytumAmountStats Double
+amountStats xs = DaytumAmountStats { least = minimum stats, most = maximum stats, average = avg }
+ where
+ stats = fieldStats amount xs
+ xs' = map amount xs
+ avg = sum xs' / fromIntegral (length xs)
+
+data DaytumDateStats = DaytumDateStats { first :: DateTime,
+ last :: DateTime,
+ averageDaysBetween :: Double
+ } deriving Show
+
+dateStats :: [DaytumRecord] -> DaytumDateStats
+dateStats xs = DaytumDateStats { first = minimum stats, last = maximum stats, averageDaysBetween = 0.0 }
+ where stats = fieldStats date xs

0 comments on commit d02ab05

Please sign in to comment.