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 , (+), (*), (/), (-), (.), (>=), ($), (**), (^), (<), (>)
33 -- TODO import functions optimized in Utils.Count
34 -- import Protolude hiding (head, last, all, any, sum, product, length)
35 -- import Gargantext.Utils.Count
36 import qualified Data.List as L hiding (head, sum)
37 import qualified Control.Monad as M
38 import qualified Data.Map as Map
39 import Data.Map.Strict (insertWith)
40 import qualified Data.Vector as V
42 import Text.Show (Show(), show)
43 import Text.Read (Read())
44 --pf :: (a -> Bool) -> [a] -> [a]
50 --pm :: (a -> b) -> [a] -> [b]
53 map2 :: (t -> b) -> [[t]] -> [[b]]
54 map2 fun = map (map fun)
56 pz :: [a] -> [b] -> [(a, b)]
59 pd :: Int -> [a] -> [a]
62 ptk :: Int -> [a] -> [a]
65 pzw :: (a -> b -> c) -> [a] -> [b] -> [c]
68 -- Exponential Average
69 eavg :: [Double] -> Double
70 eavg (x:xs) = a*x + (1-a)*(eavg xs)
75 mean :: Fractional a => [a] -> a
76 mean xs = if L.null xs then 0.0
77 else sum xs / fromIntegral (length xs)
79 sumMaybe :: Num a => [Maybe a] -> Maybe a
80 sumMaybe = fmap sum . M.sequence
82 variance :: Floating a => [a] -> a
83 variance xs = mean $ map (\x -> (x - m) ** 2) xs where
86 deviation :: [Double] -> Double
87 deviation = sqrt . variance
89 movingAverage :: Fractional b => Int -> [b] -> [b]
90 movingAverage steps xs = map mean $ chunkAlong steps 1 xs
92 ma :: [Double] -> [Double]
96 -- | Function to split a range into chunks
97 chunkAlong :: Int -> Int -> [a] -> [[a]]
98 chunkAlong a b l = only (while dropAlong)
101 while = takeWhile (\x -> length x >= a)
102 dropAlong = L.scanl (\x _y -> drop b x) l ([1..] :: [Integer])
104 -- | Optimized version (Vector)
105 chunkAlong' :: Int -> Int -> V.Vector a -> V.Vector (V.Vector a)
106 chunkAlong' a b l = only (while dropAlong)
108 only = V.map (V.take a)
109 while = V.takeWhile (\x -> V.length x >= a)
110 dropAlong = V.scanl (\x _y -> V.drop b x) l (V.fromList [1..])
112 -- | TODO Inverse of chunk ? unchunkAlong ?
113 unchunkAlong :: Int -> Int -> [[a]] -> [a]
114 unchunkAlong = undefined
117 -- splitAlong [2,3,4] ("helloworld" :: [Char]) == ["he", "llo", "worl", "d"]
118 splitAlong :: [Int] -> [Char] -> [[Char]]
119 splitAlong _ [] = [] -- No list? done
120 splitAlong [] xs = [xs] -- No place to split at? Return the remainder
121 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
123 takeWhileM :: (Monad m) => (a -> Bool) -> [m a] -> m [a]
124 takeWhileM _ [] = return []
125 takeWhileM p (a:as) = do
129 vs <- takeWhileM p as
134 -- To select the right algorithme according to the type:
135 -- https://github.com/mikeizbicki/ifcxt
137 sumSimple :: Num a => [a] -> a
138 sumSimple = L.foldl' (+) 0
140 -- | https://en.wikipedia.org/wiki/Kahan_summation_algorithm
141 sumKahan :: Num a => [a] -> a
142 sumKahan = snd . L.foldl' go (0,0)
144 go (c,t) i = ((t'-t)-y,t')
149 -- | compute part of the dict
150 count2map :: (Ord k, Foldable t) => t k -> Map.Map k Double
151 count2map xs = Map.map (/ (fromIntegral (length xs))) (count2map' xs)
153 -- | insert in a dict
154 count2map' :: (Ord k, Foldable t) => t k -> Map.Map k Double
155 count2map' xs = L.foldl' (\x y -> insertWith (+) y 1 x) Map.empty xs
158 trunc :: (RealFrac a, Integral c, Integral b) => b -> a -> c
159 trunc n = truncate . (* 10^n)
161 trunc' :: Int -> Double -> Double
162 trunc' n x = fromIntegral $ truncate $ (x * 10^n)
165 bool2int :: Num a => Bool -> a
166 bool2int b = case b of
170 bool2double :: Bool -> Double
171 bool2double bool = case bool of
177 -- Normalizing && scaling data
178 scale :: [Double] -> [Double]
181 scaleMinMax :: [Double] -> [Double]
182 scaleMinMax xs = map (\x -> (x - mi / (ma - mi + 1) )) xs'
188 scaleNormalize :: [Double] -> [Double]
189 scaleNormalize xs = map (\x -> (x - v / (m + 1))) xs'
197 normalize :: [Double] -> [Double]
198 normalize as = normalizeWith identity as
200 normalizeWith :: Fractional b => (a -> b) -> [a] -> [b]
201 normalizeWith extract bs = map (\x -> x/(sum bs')) bs'
205 -- Zip functions to add
206 zipFst :: ([b] -> [a]) -> [b] -> [(a, b)]
207 zipFst f xs = zip (f xs) xs
209 zipSnd :: ([a] -> [b]) -> [a] -> [(a, b)]
210 zipSnd f xs = zip xs (f xs)