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 , (+), (*), (/), (-), (.), (>=), ($), (**), (^), (<), (>), (==), (<>)
31 -- TODO import functions optimized in Utils.Count
32 -- import Protolude hiding (head, last, all, any, sum, product, length)
33 -- import 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 Data.Map.Strict (insertWith)
39 import qualified Data.Vector as V
41 import Text.Show (Show(), show)
42 import Text.Read (Read())
43 --pf :: (a -> Bool) -> [a] -> [a]
49 --pm :: (a -> b) -> [a] -> [b]
52 map2 :: (t -> b) -> [[t]] -> [[b]]
53 map2 fun = map (map fun)
55 pz :: [a] -> [b] -> [(a, b)]
58 pd :: Int -> [a] -> [a]
61 ptk :: Int -> [a] -> [a]
64 pzw :: (a -> b -> c) -> [a] -> [b] -> [c]
67 -- Exponential Average
68 eavg :: [Double] -> Double
69 eavg (x:xs) = a*x + (1-a)*(eavg xs)
74 mean :: Fractional a => [a] -> a
75 mean xs = if L.null xs then 0.0
76 else sum xs / fromIntegral (length xs)
78 sumMaybe :: Num a => [Maybe a] -> Maybe a
79 sumMaybe = fmap sum . M.sequence
81 variance :: Floating a => [a] -> a
82 variance xs = mean $ map (\x -> (x - m) ** 2) xs where
85 deviation :: [Double] -> Double
86 deviation = sqrt . variance
88 movingAverage :: Fractional b => Int -> [b] -> [b]
89 movingAverage steps xs = map mean $ chunkAlong steps 1 xs
91 ma :: [Double] -> [Double]
95 -- | Function to split a range into chunks
96 chunkAlong :: Int -> Int -> [a] -> [[a]]
97 chunkAlong a b l = only (while dropAlong)
100 while = takeWhile (\x -> length x >= a)
101 dropAlong = L.scanl (\x _y -> drop b x) l ([1..] :: [Integer])
103 -- | Optimized version (Vector)
104 chunkAlong' :: Int -> Int -> V.Vector a -> V.Vector (V.Vector a)
105 chunkAlong' a b l = only (while dropAlong)
107 only = V.map (V.take a)
108 while = V.takeWhile (\x -> V.length x >= a)
109 dropAlong = V.scanl (\x _y -> V.drop b x) l (V.fromList [1..])
111 -- | TODO Inverse of chunk ? unchunkAlong ?
112 unchunkAlong :: Int -> Int -> [[a]] -> [a]
113 unchunkAlong = undefined
116 -- splitAlong [2,3,4] ("helloworld" :: [Char]) == ["he", "llo", "worl", "d"]
117 splitAlong :: [Int] -> [Char] -> [[Char]]
118 splitAlong _ [] = [] -- No list? done
119 splitAlong [] xs = [xs] -- No place to split at? Return the remainder
120 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
122 takeWhileM :: (Monad m) => (a -> Bool) -> [m a] -> m [a]
123 takeWhileM _ [] = return []
124 takeWhileM p (a:as) = do
128 vs <- takeWhileM p as
133 -- To select the right algorithme according to the type:
134 -- https://github.com/mikeizbicki/ifcxt
136 sumSimple :: Num a => [a] -> a
137 sumSimple = L.foldl' (+) 0
139 -- | https://en.wikipedia.org/wiki/Kahan_summation_algorithm
140 sumKahan :: Num a => [a] -> a
141 sumKahan = snd . L.foldl' go (0,0)
143 go (c,t) i = ((t'-t)-y,t')
148 -- | compute part of the dict
149 count2map :: (Ord k, Foldable t) => t k -> Map.Map k Double
150 count2map xs = Map.map (/ (fromIntegral (length xs))) (count2map' xs)
152 -- | insert in a dict
153 count2map' :: (Ord k, Foldable t) => t k -> Map.Map k Double
154 count2map' xs = L.foldl' (\x y -> insertWith (+) y 1 x) Map.empty xs
157 trunc :: (RealFrac a, Integral c, Integral b) => b -> a -> c
158 trunc n = truncate . (* 10^n)
160 trunc' :: Int -> Double -> Double
161 trunc' n x = fromIntegral $ truncate $ (x * 10^n)
164 bool2int :: Num a => Bool -> a
165 bool2int b = case b of
169 bool2double :: Bool -> Double
170 bool2double bool = case bool of
176 -- Normalizing && scaling data
177 scale :: [Double] -> [Double]
180 scaleMinMax :: [Double] -> [Double]
181 scaleMinMax xs = map (\x -> (x - mi / (ma - mi + 1) )) xs'
187 scaleNormalize :: [Double] -> [Double]
188 scaleNormalize xs = map (\x -> (x - v / (m + 1))) xs'
196 normalize :: [Double] -> [Double]
197 normalize as = normalizeWith identity as
199 normalizeWith :: Fractional b => (a -> b) -> [a] -> [b]
200 normalizeWith extract bs = map (\x -> x/(sum bs')) bs'
204 -- Zip functions to add
205 zipFst :: ([b] -> [a]) -> [b] -> [(a, b)]
206 zipFst f xs = zip (f xs) xs
208 zipSnd :: ([a] -> [b]) -> [a] -> [(a, b)]
209 zipSnd f xs = zip xs (f xs)