1 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
2 {-# OPTIONS_GHC -fno-warn-type-defaults #-}
3 {-# LANGUAGE NoImplicitPrelude #-}
6 TODO: import head impossible from Protolude: why ?
9 module Gargantext.Prelude
10 ( module Gargantext.Prelude
18 import Protolude ( Bool(True, False), Int, Double, Integer
19 , Fractional, Num, Maybe(Just,Nothing)
22 , Ord, Integral, Foldable, RealFrac, Monad, filter
23 , reverse, map, zip, drop, take, zipWith
24 , sum, fromIntegral, length, fmap
25 , takeWhile, sqrt, undefined, identity
26 , abs, maximum, minimum, return, snd, truncate
27 , (+), (*), (/), (-), (.), (>=), ($), (**), (^), (<), (>), (==)
30 -- TODO import functions optimized in Utils.Count
31 -- import Protolude hiding (head, last, all, any, sum, product, length)
32 -- import Gargantext.Utils.Count
34 import qualified Data.List as L hiding (head, sum)
35 import qualified Control.Monad as M
36 import qualified Data.Map as Map
37 import qualified Data.Vector as V
39 import Text.Show (Show(), show)
40 import Text.Read (Read())
41 --pf :: (a -> Bool) -> [a] -> [a]
47 --pm :: (a -> b) -> [a] -> [b]
50 map2 :: (t -> b) -> [[t]] -> [[b]]
51 map2 fun = map (map fun)
53 pz :: [a] -> [b] -> [(a, b)]
56 pd :: Int -> [a] -> [a]
59 ptk :: Int -> [a] -> [a]
62 pzw :: (a -> b -> c) -> [a] -> [b] -> [c]
65 -- Exponential Average
66 eavg :: [Double] -> Double
67 eavg (x:xs) = a*x + (1-a)*(eavg xs)
72 mean :: Fractional a => [a] -> a
73 mean xs = if L.null xs then 0.0
74 else sum xs / fromIntegral (length xs)
76 sumMaybe :: Num a => [Maybe a] -> Maybe a
77 sumMaybe = fmap sum . M.sequence
79 variance :: Floating a => [a] -> a
80 variance xs = mean $ map (\x -> (x - m) ** 2) xs where
83 deviation :: [Double] -> Double
84 deviation = sqrt . variance
86 movingAverage :: Fractional b => Int -> [b] -> [b]
87 movingAverage steps xs = map mean $ chunkAlong steps 1 xs
89 ma :: [Double] -> [Double]
93 -- | Function to split a range into chunks
94 chunkAlong :: Int -> Int -> [a] -> [[a]]
95 chunkAlong a b l = only (while dropAlong)
98 while = takeWhile (\x -> length x >= a)
99 dropAlong = L.scanl (\x _y -> drop b x) l ([1..] :: [Integer])
101 -- | Optimized version (Vector)
102 chunkAlong' :: Int -> Int -> V.Vector a -> V.Vector (V.Vector a)
103 chunkAlong' a b l = only (while dropAlong)
105 only = V.map (V.take a)
106 while = V.takeWhile (\x -> V.length x >= a)
107 dropAlong = V.scanl (\x _y -> V.drop b x) l (V.fromList [1..])
109 -- | TODO Inverse of chunk ? unchunkAlong ?
110 unchunkAlong :: Int -> Int -> [[a]] -> [a]
111 unchunkAlong = undefined
114 -- splitAlong [2,3,4] ("helloworld" :: [Char]) == ["he", "llo", "worl", "d"]
115 splitAlong :: [Int] -> [Char] -> [[Char]]
116 splitAlong _ [] = [] -- No list? done
117 splitAlong [] xs = [xs] -- No place to split at? Return the remainder
118 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
120 takeWhileM :: (Monad m) => (a -> Bool) -> [m a] -> m [a]
121 takeWhileM _ [] = return []
122 takeWhileM p (a:as) = do
126 vs <- takeWhileM p as
131 -- To select the right algorithme according to the type:
132 -- https://github.com/mikeizbicki/ifcxt
134 sumSimple :: Num a => [a] -> a
135 sumSimple = L.foldl' (+) 0
137 -- | https://en.wikipedia.org/wiki/Kahan_summation_algorithm
138 sumKahan :: Num a => [a] -> a
139 sumKahan = snd . L.foldl' go (0,0)
141 go (c,t) i = ((t'-t)-y,t')
146 -- | compute part of the dict
147 count2map :: (Ord k, Foldable t) => t k -> Map.Map k Double
148 count2map xs = Map.map (/ (fromIntegral (length xs))) (count2map' xs)
150 -- | insert in a dict
151 count2map' :: (Ord k, Foldable t) => t k -> Map.Map k Double
152 count2map' xs = L.foldl' (\x y -> Map.insertWith' (+) y 1 x) Map.empty xs
155 trunc :: (RealFrac a, Integral c, Integral b) => b -> a -> c
156 trunc n = truncate . (* 10^n)
158 trunc' :: Int -> Double -> Double
159 trunc' n x = fromIntegral $ truncate $ (x * 10^n)
162 bool2int :: Num a => Bool -> a
163 bool2int b = case b of
167 bool2double :: Bool -> Double
168 bool2double bool = case bool of
174 -- Normalizing && scaling data
175 scale :: [Double] -> [Double]
178 scaleMinMax :: [Double] -> [Double]
179 scaleMinMax xs = map (\x -> (x - mi / (ma - mi + 1) )) xs'
185 scaleNormalize :: [Double] -> [Double]
186 scaleNormalize xs = map (\x -> (x - v / (m + 1))) xs'
194 normalize :: [Double] -> [Double]
195 normalize as = normalizeWith identity as
197 normalizeWith :: Fractional b => (a -> b) -> [a] -> [b]
198 normalizeWith extract bs = map (\x -> x/(sum bs')) bs'
202 -- Zip functions to add
203 zipFst :: ([b] -> [a]) -> [b] -> [(a, b)]
204 zipFst f xs = zip (f xs) xs
206 zipSnd :: ([a] -> [b]) -> [a] -> [(a, b)]
207 zipSnd f xs = zip xs (f xs)