2 Module : Gargantext.Prelude
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Here is a longer description of this module, containing some
11 commentary with @some markup@.
14 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
15 {-# OPTIONS_GHC -fno-warn-type-defaults #-}
17 {-# LANGUAGE NoImplicitPrelude #-}
19 module Gargantext.Prelude
20 ( module Gargantext.Prelude
29 import Data.Maybe (isJust, fromJust)
30 import Protolude ( Bool(True, False), Int, Double, Integer
31 , Fractional, Num, Maybe(Just,Nothing)
35 , Ord, Integral, Foldable, RealFrac, Monad, filter
36 , reverse, map, zip, drop, take, zipWith
37 , sum, fromIntegral, length, fmap
38 , takeWhile, sqrt, undefined, identity
39 , abs, maximum, minimum, return, snd, truncate
40 , (+), (*), (/), (-), (.), ($), (**), (^), (<), (>)
41 , Eq, (==), (>=), (<=), (<>)
47 -- TODO import functions optimized in Utils.Count
48 -- import Protolude hiding (head, last, all, any, sum, product, length)
49 -- import Gargantext.Utils.Count
50 import qualified Data.List as L hiding (head, sum)
51 import qualified Control.Monad as M
52 import qualified Data.Map as Map
53 import Data.Map.Strict (insertWith)
54 import qualified Data.Vector as V
56 import Text.Show (Show(), show)
57 import Text.Read (Read())
58 import Data.String.Conversions (cs)
60 --pf :: (a -> Bool) -> [a] -> [a]
66 --pm :: (a -> b) -> [a] -> [b]
69 map2 :: (t -> b) -> [[t]] -> [[b]]
70 map2 fun = map (map fun)
72 pz :: [a] -> [b] -> [(a, b)]
75 pd :: Int -> [a] -> [a]
78 ptk :: Int -> [a] -> [a]
81 pzw :: (a -> b -> c) -> [a] -> [b] -> [c]
84 -- Exponential Average
85 eavg :: [Double] -> Double
86 eavg (x:xs) = a*x + (1-a)*(eavg xs)
91 mean :: Fractional a => [a] -> a
92 mean xs = if L.null xs then 0.0
93 else sum xs / fromIntegral (length xs)
95 sumMaybe :: Num a => [Maybe a] -> Maybe a
96 sumMaybe = fmap sum . M.sequence
98 variance :: Floating a => [a] -> a
99 variance xs = mean $ map (\x -> (x - m) ** 2) xs where
102 deviation :: [Double] -> Double
103 deviation = sqrt . variance
105 movingAverage :: Fractional b => Int -> [b] -> [b]
106 movingAverage steps xs = map mean $ chunkAlong steps 1 xs
108 ma :: [Double] -> [Double]
112 -- | Function to split a range into chunks
113 chunkAlong :: Int -> Int -> [a] -> [[a]]
114 chunkAlong a b l = only (while dropAlong)
117 while = takeWhile (\x -> length x >= a)
118 dropAlong = L.scanl (\x _y -> drop b x) l ([1..] :: [Integer])
120 -- | Optimized version (Vector)
121 chunkAlong' :: Int -> Int -> V.Vector a -> V.Vector (V.Vector a)
122 chunkAlong' a b l = only (while dropAlong)
124 only = V.map (V.take a)
125 while = V.takeWhile (\x -> V.length x >= a)
126 dropAlong = V.scanl (\x _y -> V.drop b x) l (V.fromList [1..])
128 -- | TODO Inverse of chunk ? unchunkAlong ?
129 unchunkAlong :: Int -> Int -> [[a]] -> [a]
130 unchunkAlong = undefined
133 -- splitAlong [2,3,4] ("helloworld" :: [Char]) == ["he", "llo", "worl", "d"]
134 splitAlong :: [Int] -> [Char] -> [[Char]]
135 splitAlong _ [] = [] -- No list? done
136 splitAlong [] xs = [xs] -- No place to split at? Return the remainder
137 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
139 takeWhileM :: (Monad m) => (a -> Bool) -> [m a] -> m [a]
140 takeWhileM _ [] = return []
141 takeWhileM p (a:as) = do
145 vs <- takeWhileM p as
150 -- To select the right algorithme according to the type:
151 -- https://github.com/mikeizbicki/ifcxt
153 sumSimple :: Num a => [a] -> a
154 sumSimple = L.foldl' (+) 0
156 -- | https://en.wikipedia.org/wiki/Kahan_summation_algorithm
157 sumKahan :: Num a => [a] -> a
158 sumKahan = snd . L.foldl' go (0,0)
160 go (c,t) i = ((t'-t)-y,t')
165 -- | compute part of the dict
166 count2map :: (Ord k, Foldable t) => t k -> Map.Map k Double
167 count2map xs = Map.map (/ (fromIntegral (length xs))) (count2map' xs)
169 -- | insert in a dict
170 count2map' :: (Ord k, Foldable t) => t k -> Map.Map k Double
171 count2map' xs = L.foldl' (\x y -> insertWith (+) y 1 x) Map.empty xs
174 trunc :: (RealFrac a, Integral c, Integral b) => b -> a -> c
175 trunc n = truncate . (* 10^n)
177 trunc' :: Int -> Double -> Double
178 trunc' n x = fromIntegral $ truncate $ (x * 10^n)
181 bool2int :: Num a => Bool -> a
182 bool2int b = case b of
186 bool2double :: Bool -> Double
187 bool2double bool = case bool of
193 -- Normalizing && scaling data
194 scale :: [Double] -> [Double]
197 scaleMinMax :: [Double] -> [Double]
198 scaleMinMax xs = map (\x -> (x - mi / (ma - mi + 1) )) xs'
204 scaleNormalize :: [Double] -> [Double]
205 scaleNormalize xs = map (\x -> (x - v / (m + 1))) xs'
213 normalize :: [Double] -> [Double]
214 normalize as = normalizeWith identity as
216 normalizeWith :: Fractional b => (a -> b) -> [a] -> [b]
217 normalizeWith extract bs = map (\x -> x/(sum bs')) bs'
221 -- Zip functions to add
222 zipFst :: ([b] -> [a]) -> [b] -> [(a, b)]
223 zipFst f xs = zip (f xs) xs
225 zipSnd :: ([a] -> [b]) -> [a] -> [(a, b)]
226 zipSnd f xs = zip xs (f xs)
231 unMaybe :: [Maybe a] -> [a]
232 unMaybe = map fromJust . L.filter isJust