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
19 import Data.Maybe (isJust, fromJust)
20 import Protolude ( Bool(True, False), Int, Double, Integer
21 , Fractional, Num, Maybe(Just,Nothing)
25 , Ord, Integral, Foldable, RealFrac, Monad, filter
26 , reverse, map, zip, drop, take, zipWith
27 , sum, fromIntegral, length, fmap
28 , takeWhile, sqrt, undefined, identity
29 , abs, maximum, minimum, return, snd, truncate
30 , (+), (*), (/), (-), (.), ($), (**), (^), (<), (>)
31 , Eq, (==), (>=), (<=), (<>)
37 -- TODO import functions optimized in Utils.Count
38 -- import Protolude hiding (head, last, all, any, sum, product, length)
39 -- import Gargantext.Utils.Count
40 import qualified Data.List as L hiding (head, sum)
41 import qualified Control.Monad as M
42 import qualified Data.Map as Map
43 import Data.Map.Strict (insertWith)
44 import qualified Data.Vector as V
46 import Text.Show (Show(), show)
47 import Text.Read (Read())
48 import Data.String.Conversions (cs)
50 --pf :: (a -> Bool) -> [a] -> [a]
56 --pm :: (a -> b) -> [a] -> [b]
59 map2 :: (t -> b) -> [[t]] -> [[b]]
60 map2 fun = map (map fun)
62 pz :: [a] -> [b] -> [(a, b)]
65 pd :: Int -> [a] -> [a]
68 ptk :: Int -> [a] -> [a]
71 pzw :: (a -> b -> c) -> [a] -> [b] -> [c]
74 -- Exponential Average
75 eavg :: [Double] -> Double
76 eavg (x:xs) = a*x + (1-a)*(eavg xs)
81 mean :: Fractional a => [a] -> a
82 mean xs = if L.null xs then 0.0
83 else sum xs / fromIntegral (length xs)
85 sumMaybe :: Num a => [Maybe a] -> Maybe a
86 sumMaybe = fmap sum . M.sequence
88 variance :: Floating a => [a] -> a
89 variance xs = mean $ map (\x -> (x - m) ** 2) xs where
92 deviation :: [Double] -> Double
93 deviation = sqrt . variance
95 movingAverage :: Fractional b => Int -> [b] -> [b]
96 movingAverage steps xs = map mean $ chunkAlong steps 1 xs
98 ma :: [Double] -> [Double]
102 -- | Function to split a range into chunks
103 chunkAlong :: Int -> Int -> [a] -> [[a]]
104 chunkAlong a b l = only (while dropAlong)
107 while = takeWhile (\x -> length x >= a)
108 dropAlong = L.scanl (\x _y -> drop b x) l ([1..] :: [Integer])
110 -- | Optimized version (Vector)
111 chunkAlong' :: Int -> Int -> V.Vector a -> V.Vector (V.Vector a)
112 chunkAlong' a b l = only (while dropAlong)
114 only = V.map (V.take a)
115 while = V.takeWhile (\x -> V.length x >= a)
116 dropAlong = V.scanl (\x _y -> V.drop b x) l (V.fromList [1..])
118 -- | TODO Inverse of chunk ? unchunkAlong ?
119 unchunkAlong :: Int -> Int -> [[a]] -> [a]
120 unchunkAlong = undefined
123 -- splitAlong [2,3,4] ("helloworld" :: [Char]) == ["he", "llo", "worl", "d"]
124 splitAlong :: [Int] -> [Char] -> [[Char]]
125 splitAlong _ [] = [] -- No list? done
126 splitAlong [] xs = [xs] -- No place to split at? Return the remainder
127 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
129 takeWhileM :: (Monad m) => (a -> Bool) -> [m a] -> m [a]
130 takeWhileM _ [] = return []
131 takeWhileM p (a:as) = do
135 vs <- takeWhileM p as
140 -- To select the right algorithme according to the type:
141 -- https://github.com/mikeizbicki/ifcxt
143 sumSimple :: Num a => [a] -> a
144 sumSimple = L.foldl' (+) 0
146 -- | https://en.wikipedia.org/wiki/Kahan_summation_algorithm
147 sumKahan :: Num a => [a] -> a
148 sumKahan = snd . L.foldl' go (0,0)
150 go (c,t) i = ((t'-t)-y,t')
155 -- | compute part of the dict
156 count2map :: (Ord k, Foldable t) => t k -> Map.Map k Double
157 count2map xs = Map.map (/ (fromIntegral (length xs))) (count2map' xs)
159 -- | insert in a dict
160 count2map' :: (Ord k, Foldable t) => t k -> Map.Map k Double
161 count2map' xs = L.foldl' (\x y -> insertWith (+) y 1 x) Map.empty xs
164 trunc :: (RealFrac a, Integral c, Integral b) => b -> a -> c
165 trunc n = truncate . (* 10^n)
167 trunc' :: Int -> Double -> Double
168 trunc' n x = fromIntegral $ truncate $ (x * 10^n)
171 bool2int :: Num a => Bool -> a
172 bool2int b = case b of
176 bool2double :: Bool -> Double
177 bool2double bool = case bool of
183 -- Normalizing && scaling data
184 scale :: [Double] -> [Double]
187 scaleMinMax :: [Double] -> [Double]
188 scaleMinMax xs = map (\x -> (x - mi / (ma - mi + 1) )) xs'
194 scaleNormalize :: [Double] -> [Double]
195 scaleNormalize xs = map (\x -> (x - v / (m + 1))) xs'
203 normalize :: [Double] -> [Double]
204 normalize as = normalizeWith identity as
206 normalizeWith :: Fractional b => (a -> b) -> [a] -> [b]
207 normalizeWith extract bs = map (\x -> x/(sum bs')) bs'
211 -- Zip functions to add
212 zipFst :: ([b] -> [a]) -> [b] -> [(a, b)]
213 zipFst f xs = zip (f xs) xs
215 zipSnd :: ([a] -> [b]) -> [a] -> [(a, b)]
216 zipSnd f xs = zip xs (f xs)
221 unMaybe :: [Maybe a] -> [a]
222 unMaybe = map fromJust . L.filter isJust