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 Data.Maybe (isJust, fromJust)
19 import Protolude ( Bool(True, False), Int, Double, Integer
20 , Fractional, Num, Maybe(Just,Nothing)
24 , Ord, Integral, Foldable, RealFrac, Monad, filter
25 , reverse, map, zip, drop, take, zipWith
26 , sum, fromIntegral, length, fmap
27 , takeWhile, sqrt, undefined, identity
28 , abs, maximum, minimum, return, snd, truncate
29 , (+), (*), (/), (-), (.), ($), (**), (^), (<), (>)
30 , Eq, (==), (>=), (<=), (<>)
35 -- TODO import functions optimized in Utils.Count
36 -- import Protolude hiding (head, last, all, any, sum, product, length)
37 -- import Gargantext.Utils.Count
38 import qualified Data.List as L hiding (head, sum)
39 import qualified Control.Monad as M
40 import qualified Data.Map as Map
41 import Data.Map.Strict (insertWith)
42 import qualified Data.Vector as V
44 import Text.Show (Show(), show)
45 import Text.Read (Read())
46 --pf :: (a -> Bool) -> [a] -> [a]
52 --pm :: (a -> b) -> [a] -> [b]
55 map2 :: (t -> b) -> [[t]] -> [[b]]
56 map2 fun = map (map fun)
58 pz :: [a] -> [b] -> [(a, b)]
61 pd :: Int -> [a] -> [a]
64 ptk :: Int -> [a] -> [a]
67 pzw :: (a -> b -> c) -> [a] -> [b] -> [c]
70 -- Exponential Average
71 eavg :: [Double] -> Double
72 eavg (x:xs) = a*x + (1-a)*(eavg xs)
77 mean :: Fractional a => [a] -> a
78 mean xs = if L.null xs then 0.0
79 else sum xs / fromIntegral (length xs)
81 sumMaybe :: Num a => [Maybe a] -> Maybe a
82 sumMaybe = fmap sum . M.sequence
84 variance :: Floating a => [a] -> a
85 variance xs = mean $ map (\x -> (x - m) ** 2) xs where
88 deviation :: [Double] -> Double
89 deviation = sqrt . variance
91 movingAverage :: Fractional b => Int -> [b] -> [b]
92 movingAverage steps xs = map mean $ chunkAlong steps 1 xs
94 ma :: [Double] -> [Double]
98 -- | Function to split a range into chunks
99 chunkAlong :: Int -> Int -> [a] -> [[a]]
100 chunkAlong a b l = only (while dropAlong)
103 while = takeWhile (\x -> length x >= a)
104 dropAlong = L.scanl (\x _y -> drop b x) l ([1..] :: [Integer])
106 -- | Optimized version (Vector)
107 chunkAlong' :: Int -> Int -> V.Vector a -> V.Vector (V.Vector a)
108 chunkAlong' a b l = only (while dropAlong)
110 only = V.map (V.take a)
111 while = V.takeWhile (\x -> V.length x >= a)
112 dropAlong = V.scanl (\x _y -> V.drop b x) l (V.fromList [1..])
114 -- | TODO Inverse of chunk ? unchunkAlong ?
115 unchunkAlong :: Int -> Int -> [[a]] -> [a]
116 unchunkAlong = undefined
119 -- splitAlong [2,3,4] ("helloworld" :: [Char]) == ["he", "llo", "worl", "d"]
120 splitAlong :: [Int] -> [Char] -> [[Char]]
121 splitAlong _ [] = [] -- No list? done
122 splitAlong [] xs = [xs] -- No place to split at? Return the remainder
123 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
125 takeWhileM :: (Monad m) => (a -> Bool) -> [m a] -> m [a]
126 takeWhileM _ [] = return []
127 takeWhileM p (a:as) = do
131 vs <- takeWhileM p as
136 -- To select the right algorithme according to the type:
137 -- https://github.com/mikeizbicki/ifcxt
139 sumSimple :: Num a => [a] -> a
140 sumSimple = L.foldl' (+) 0
142 -- | https://en.wikipedia.org/wiki/Kahan_summation_algorithm
143 sumKahan :: Num a => [a] -> a
144 sumKahan = snd . L.foldl' go (0,0)
146 go (c,t) i = ((t'-t)-y,t')
151 -- | compute part of the dict
152 count2map :: (Ord k, Foldable t) => t k -> Map.Map k Double
153 count2map xs = Map.map (/ (fromIntegral (length xs))) (count2map' xs)
155 -- | insert in a dict
156 count2map' :: (Ord k, Foldable t) => t k -> Map.Map k Double
157 count2map' xs = L.foldl' (\x y -> insertWith (+) y 1 x) Map.empty xs
160 trunc :: (RealFrac a, Integral c, Integral b) => b -> a -> c
161 trunc n = truncate . (* 10^n)
163 trunc' :: Int -> Double -> Double
164 trunc' n x = fromIntegral $ truncate $ (x * 10^n)
167 bool2int :: Num a => Bool -> a
168 bool2int b = case b of
172 bool2double :: Bool -> Double
173 bool2double bool = case bool of
179 -- Normalizing && scaling data
180 scale :: [Double] -> [Double]
183 scaleMinMax :: [Double] -> [Double]
184 scaleMinMax xs = map (\x -> (x - mi / (ma - mi + 1) )) xs'
190 scaleNormalize :: [Double] -> [Double]
191 scaleNormalize xs = map (\x -> (x - v / (m + 1))) xs'
199 normalize :: [Double] -> [Double]
200 normalize as = normalizeWith identity as
202 normalizeWith :: Fractional b => (a -> b) -> [a] -> [b]
203 normalizeWith extract bs = map (\x -> x/(sum bs')) bs'
207 -- Zip functions to add
208 zipFst :: ([b] -> [a]) -> [b] -> [(a, b)]
209 zipFst f xs = zip (f xs) xs
211 zipSnd :: ([a] -> [b]) -> [a] -> [(a, b)]
212 zipSnd f xs = zip xs (f xs)
217 unMaybe :: [Maybe a] -> [a]
218 unMaybe = map fromJust . L.filter isJust