Commits

Aleksey Khudyakov  committed da8c84d

Add functions to fix types

  • Participants
  • Parent commits 8c769aa
  • Tags v0.2

Comments (0)

Files changed (2)

File Data/Monoid/Statistics/Numeric.hs

 module Data.Monoid.Statistics.Numeric ( 
     -- * Mean and variance
     Count(..)
+  , asCount
   , Mean(..)
+  , asMean
   , Variance(..)
+  , asVariance
     -- ** Ad-hoc accessors
   , CalcCount(..)
   , CalcMean(..)
 newtype Count a = Count { calcCountI :: a }
                   deriving Show
 
+-- | Fix type of monoid
+asCount :: Count a -> Count a
+asCount = id
+{-# INLINE asCount #-}
+
 instance Integral a => Monoid (Count a) where
   mempty = Count 0
   (Count i) `mappend` (Count j) = Count (i + j)
                  {-# UNPACK #-} !Double -- Current mean
             deriving Show
 
+-- | Fix type of monoid
+asMean :: Mean -> Mean
+asMean = id
+{-# INLINE asMean #-}
+
 instance Monoid Mean where
   mempty = Mean 0 0
   mappend !(Mean n x) !(Mean k y) = Mean (n + k) ((x*n' + y*k') / (n' + k')) 
                          {-# UNPACK #-} !Double -- Current sum of squares of deviations from current mean
                 deriving Show
 
+-- | Fix type of monoid
+asVariance :: Variance -> Variance
+asVariance = id
+{-# INLINE asVariance #-}
+
 -- | Using parallel algorithm from:
 -- 
 -- Chan, Tony F.; Golub, Gene H.; LeVeque, Randall J. (1979),

File bench/bench.hs

 
 main :: IO ()
 main = do
-  defaultMain [ bench "mean-list-D" $ nf  (calcMean . evalStatistic) sampleD 
-              , bench "mean-list-D" $ nf  (calcMean . evalStatistic) sampleI
-              , bench "var-list-D"  $ nf  (calcVariance . evalStatistic) sampleI
-              , bench "var-list-D"  $ nf  (calcVariance . evalStatistic) sampleI
+  defaultMain [ bench "mean-list-D" $ nf  (calcMean     . asMean     . evalStatistic) sampleD 
+              , bench "mean-list-I" $ nf  (calcMean     . asMean     . evalStatistic) sampleI
+              , bench "var-list-D"  $ nf  (calcVariance . asVariance . evalStatistic) sampleI
+              , bench "var-list-I"  $ nf  (calcVariance . asVariance . evalStatistic) sampleI
               ]