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
42 pf :: (a -> Bool) -> [a] -> [a]
48 pm :: (a -> b) -> [a] -> [b]
51 pm2 :: (t -> b) -> [[t]] -> [[b]]
54 pz :: [a] -> [b] -> [(a, b)]
57 pd :: Int -> [a] -> [a]
60 ptk :: Int -> [a] -> [a]
63 pzw :: (a -> b -> c) -> [a] -> [b] -> [c]
66 -- Exponential Average
67 eavg :: [Double] -> Double
68 eavg (x:xs) = a*x + (1-a)*(eavg xs)
73 mean :: Fractional a => [a] -> a
74 mean xs = if L.null xs then 0.0
75 else sum xs / fromIntegral (length xs)
77 sumMaybe :: Num a => [Maybe a] -> Maybe a
78 sumMaybe = fmap sum . M.sequence
80 variance :: Floating a => [a] -> a
81 variance xs = mean $ pm (\x -> (x - m) ** 2) xs where
84 deviation :: [Double] -> Double
85 deviation = sqrt . variance
87 movingAverage :: Fractional b => Int -> [b] -> [b]
88 movingAverage steps xs = pm mean $ chunkAlong steps 1 xs
90 ma :: [Double] -> [Double]
94 -- | Function to split a range into chunks
95 chunkAlong :: Int -> Int -> [a] -> [[a]]
96 chunkAlong a b l = only (while dropAlong)
99 while = takeWhile (\x -> length x >= a)
100 dropAlong = L.scanl (\x _y -> drop b x) l ([1..] :: [Integer])
102 -- | Optimized version (Vector)
103 chunkAlong' :: Int -> Int -> V.Vector a -> V.Vector (V.Vector a)
104 chunkAlong' a b l = only (while dropAlong)
106 only = V.map (V.take a)
107 while = V.takeWhile (\x -> V.length x >= a)
108 dropAlong = V.scanl (\x _y -> V.drop b x) l (V.fromList [1..])
110 -- | TODO Inverse of chunk ? unchunkAlong ?
111 unchunkAlong :: Int -> Int -> [[a]] -> [a]
112 unchunkAlong = undefined
115 -- splitAlong [2,3,4] ("helloworld" :: [Char]) == ["he", "llo", "worl", "d"]
116 splitAlong :: [Int] -> [Char] -> [[Char]]
117 splitAlong _ [] = [] -- No list? done
118 splitAlong [] xs = [xs] -- No place to split at? Return the remainder
119 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
121 takeWhileM :: (Monad m) => (a -> Bool) -> [m a] -> m [a]
122 takeWhileM _ [] = return []
123 takeWhileM p (a:as) = do
127 vs <- takeWhileM p as
132 -- To select the right algorithme according to the type:
133 -- https://github.com/mikeizbicki/ifcxt
135 sumSimple :: Num a => [a] -> a
136 sumSimple = L.foldl' (+) 0
138 -- | https://en.wikipedia.org/wiki/Kahan_summation_algorithm
139 sumKahan :: Num a => [a] -> a
140 sumKahan = snd . L.foldl' go (0,0)
142 go (c,t) i = ((t'-t)-y,t')
147 -- | compute part of the dict
148 count2map :: (Ord k, Foldable t) => t k -> Map.Map k Double
149 count2map xs = Map.map (/ (fromIntegral (length xs))) (count2map' xs)
151 -- | insert in a dict
152 count2map' :: (Ord k, Foldable t) => t k -> Map.Map k Double
153 count2map' xs = L.foldl' (\x y -> Map.insertWith' (+) y 1 x) Map.empty xs
156 trunc :: (RealFrac a, Integral c, Integral b) => b -> a -> c
157 trunc n = truncate . (* 10^n)
159 trunc' :: Int -> Double -> Double
160 trunc' n x = fromIntegral $ truncate $ (x * 10^n)
163 bool2int :: Num a => Bool -> a
164 bool2int b = case b of
168 bool2double :: Bool -> Double
169 bool2double bool = case bool of
175 -- Normalizing && scaling data
176 scale :: [Double] -> [Double]
179 scaleMinMax :: [Double] -> [Double]
180 scaleMinMax xs = pm (\x -> (x - mi / (ma - mi + 1) )) xs'
186 scaleNormalize :: [Double] -> [Double]
187 scaleNormalize xs = pm (\x -> (x - v / (m + 1))) xs'
195 normalize :: [Double] -> [Double]
196 normalize as = normalizeWith identity as
198 normalizeWith :: Fractional b => (a -> b) -> [a] -> [b]
199 normalizeWith extract bs = pm (\x -> x/(sum bs')) bs'
203 -- Zip functions to add
204 zipFst :: ([b] -> [a]) -> [b] -> [(a, b)]
205 zipFst f xs = zip (f xs) xs
207 zipSnd :: ([a] -> [b]) -> [a] -> [(a, b)]
208 zipSnd f xs = zip xs (f xs)