1 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
2 {-# OPTIONS_GHC -fno-warn-type-defaults #-}
3 {-# LANGUAGE NoImplicitPrelude #-}
5 module Data.Gargantext.Prelude where
7 import Protolude (Bool(True, False), Int, Double, Integer, Fractional, Num, Maybe, Floating, Char, Ord, Integral, Foldable, RealFrac, Monad, filter,
28 , (+), (*), (/), (-), (.), (>=), ($), (**), (^)
31 -- TODO import functions optimized in Utils.Count
32 -- import Protolude hiding (head, last, all, any, sum, product, length)
33 -- import Data.Gargantext.Utils.Count
35 import qualified Data.List as L hiding (head, sum)
36 import qualified Control.Monad as M
37 import qualified Data.Map as Map
38 import qualified Data.Vector as V
40 pf :: (a -> Bool) -> [a] -> [a]
46 pm :: (a -> b) -> [a] -> [b]
49 pm2 :: (t -> b) -> [[t]] -> [[b]]
52 pz :: [a] -> [b] -> [(a, b)]
55 pd :: Int -> [a] -> [a]
58 ptk :: Int -> [a] -> [a]
61 pzw :: (a -> b -> c) -> [a] -> [b] -> [c]
64 -- Exponential Average
65 eavg :: [Double] -> Double
66 eavg (x:xs) = a*x + (1-a)*(eavg xs)
71 mean :: Fractional a => [a] -> a
72 mean xs = if L.null xs then 0.0
73 else sum xs / fromIntegral (length xs)
75 sumMaybe :: Num a => [Maybe a] -> Maybe a
76 sumMaybe = fmap sum . M.sequence
78 variance :: Floating a => [a] -> a
79 variance xs = mean $ pm (\x -> (x - m) ** 2) xs where
82 deviation :: [Double] -> Double
83 deviation = sqrt . variance
85 movingAverage :: Fractional b => Int -> [b] -> [b]
86 movingAverage steps xs = pm mean $ chunkAlong steps 1 xs
88 ma :: [Double] -> [Double]
92 -- | Function to split a range into chunks
93 chunkAlong :: Int -> Int -> [a] -> [[a]]
94 chunkAlong a b l = only (while dropAlong)
97 while = takeWhile (\x -> length x >= a)
98 dropAlong = L.scanl (\x _y -> drop b x) l ([1..] :: [Integer])
100 -- | Optimized version (Vector)
101 chunkAlong' :: Int -> Int -> V.Vector a -> V.Vector (V.Vector a)
102 chunkAlong' a b l = only (while dropAlong)
104 only = V.map (V.take a)
105 while = V.takeWhile (\x -> V.length x >= a)
106 dropAlong = V.scanl (\x _y -> V.drop b x) l (V.fromList [1..])
108 -- | TODO Inverse of chunk ? unchunkAlong ?
109 unchunkAlong :: Int -> Int -> [[a]] -> [a]
110 unchunkAlong = undefined
113 -- splitAlong [2,3,4] ("helloworld" :: [Char]) == ["he", "llo", "worl", "d"]
114 splitAlong :: [Int] -> [Char] -> [[Char]]
115 splitAlong _ [] = [] -- No list? done
116 splitAlong [] xs = [xs] -- No place to split at? Return the remainder
117 splitAlong (x:xs) ys = take x ys : splitAlong xs (drop x ys) -- take until our split spot, recurse with next split spot and list remainder
119 takeWhileM :: (Monad m) => (a -> Bool) -> [m a] -> m [a]
120 takeWhileM _ [] = return []
121 takeWhileM p (a:as) = do
125 vs <- takeWhileM p as
130 -- To select the right algorithme according to the type:
131 -- https://github.com/mikeizbicki/ifcxt
133 sumSimple :: Num a => [a] -> a
134 sumSimple = L.foldl' (+) 0
136 -- | https://en.wikipedia.org/wiki/Kahan_summation_algorithm
137 sumKahan :: Num a => [a] -> a
138 sumKahan = snd . L.foldl' go (0,0)
140 go (c,t) i = ((t'-t)-y,t')
145 -- | compute part of the dict
146 count2map :: (Ord k, Foldable t) => t k -> Map.Map k Double
147 count2map xs = Map.map (/ (fromIntegral (length xs))) (count2map' xs)
149 -- | insert in a dict
150 count2map' :: (Ord k, Foldable t) => t k -> Map.Map k Double
151 count2map' xs = L.foldl' (\x y -> Map.insertWith' (+) y 1 x) Map.empty xs
154 trunc :: (RealFrac a, Integral c, Integral b) => b -> a -> c
155 trunc n = truncate . (* 10^n)
157 trunc' :: Int -> Double -> Double
158 trunc' n x = fromIntegral $ truncate $ (x * 10^n)
161 bool2int :: Num a => Bool -> a
162 bool2int b = case b of
166 bool2double :: Bool -> Double
167 bool2double bool = case bool of
173 -- Normalizing && scaling data
174 scale :: [Double] -> [Double]
177 scaleMinMax :: [Double] -> [Double]
178 scaleMinMax xs = pm (\x -> (x - mi / (ma - mi + 1) )) xs'
184 scaleNormalize :: [Double] -> [Double]
185 scaleNormalize xs = pm (\x -> (x - v / (m + 1))) xs'
193 normalize :: [Double] -> [Double]
194 normalize as = normalizeWith identity as
196 normalizeWith :: Fractional b => (a -> b) -> [a] -> [b]
197 normalizeWith extract bs = pm (\x -> x/(sum bs')) bs'
201 -- Zip functions to add
202 zipFst :: ([b] -> [a]) -> [b] -> [(a, b)]
203 zipFst f xs = zip (f xs) xs
205 zipSnd :: ([a] -> [b]) -> [a] -> [(a, b)]
206 zipSnd f xs = zip xs (f xs)