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
30 import Data.Maybe (isJust, fromJust, maybe)
31 import Protolude ( Bool(True, False), Int, Double, Integer
32 , Fractional, Num, Maybe(Just,Nothing)
33 , Enum, Bounded, Float
35 , pure, (<*>), (<$>), panic
38 , Ord, Integral, Foldable, RealFrac, Monad, filter
39 , reverse, map, zip, drop, take, zipWith
40 , sum, fromIntegral, length, fmap, foldl, foldl'
41 , takeWhile, sqrt, undefined, identity
42 , abs, min, max, maximum, minimum, return, snd, truncate
43 , (+), (*), (/), (-), (.), ($), (**), (^), (<), (>), log
44 , Eq, (==), (>=), (<=), (<>), (/=)
47 , elem, die, mod, div, const, either
52 -- TODO import functions optimized in Utils.Count
53 -- import Protolude hiding (head, last, all, any, sum, product, length)
54 -- import Gargantext.Utils.Count
55 import qualified Data.List as L hiding (head, sum)
56 import qualified Control.Monad as M
59 import qualified Data.Map as M
61 import Data.Map.Strict (insertWith)
62 import qualified Data.Vector as V
64 import Text.Show (Show(), show)
65 import Text.Read (Read())
66 import Data.String.Conversions (cs)
68 --pf :: (a -> Bool) -> [a] -> [a]
74 --pm :: (a -> b) -> [a] -> [b]
77 map2 :: (t -> b) -> [[t]] -> [[b]]
78 map2 fun = map (map fun)
80 -- Exponential Average
81 eavg :: [Double] -> Double
82 eavg (x:xs) = a*x + (1-a)*(eavg xs)
87 mean :: Fractional a => [a] -> a
88 mean xs = if L.null xs then 0.0
89 else sum xs / fromIntegral (length xs)
91 sumMaybe :: Num a => [Maybe a] -> Maybe a
92 sumMaybe = fmap sum . M.sequence
94 variance :: Floating a => [a] -> a
95 variance xs = mean $ map (\x -> (x - m) ** 2) xs where
98 deviation :: [Double] -> Double
99 deviation = sqrt . variance
101 movingAverage :: Fractional b => Int -> [b] -> [b]
102 movingAverage steps xs = map mean $ chunkAlong steps 1 xs
104 ma :: [Double] -> [Double]
108 -- | Function to split a range into chunks
109 chunkAlong :: Int -> Int -> [a] -> [[a]]
110 chunkAlong a b l = only (while dropAlong)
113 while = takeWhile (\x -> length x >= a)
114 dropAlong = L.scanl (\x _y -> drop b x) l ([1..] :: [Integer])
116 -- | Optimized version (Vector)
117 chunkAlong' :: Int -> Int -> V.Vector a -> V.Vector (V.Vector a)
118 chunkAlong' a b l = only (while dropAlong)
120 only = V.map (V.take a)
121 while = V.takeWhile (\x -> V.length x >= a)
122 dropAlong = V.scanl (\x _y -> V.drop b x) l (V.fromList [1..])
124 -- | TODO Inverse of chunk ? unchunkAlong ?
125 unchunkAlong :: Int -> Int -> [[a]] -> [a]
126 unchunkAlong = undefined
129 -- splitAlong [2,3,4] ("helloworld" :: [Char]) == ["he", "llo", "worl", "d"]
130 splitAlong :: [Int] -> [Char] -> [[Char]]
131 splitAlong _ [] = [] -- No list? done
132 splitAlong [] xs = [xs] -- No place to split at? Return the remainder
133 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
135 takeWhileM :: (Monad m) => (a -> Bool) -> [m a] -> m [a]
136 takeWhileM _ [] = return []
137 takeWhileM p (a:as) = do
141 vs <- takeWhileM p as
146 -- To select the right algorithme according to the type:
147 -- https://github.com/mikeizbicki/ifcxt
149 sumSimple :: Num a => [a] -> a
150 sumSimple = L.foldl' (+) 0
152 -- | https://en.wikipedia.org/wiki/Kahan_summation_algorithm
153 sumKahan :: Num a => [a] -> a
154 sumKahan = snd . L.foldl' go (0,0)
156 go (c,t) i = ((t'-t)-y,t')
161 -- | compute part of the dict
162 count2map :: (Ord k, Foldable t) => t k -> Map k Double
163 count2map xs = M.map (/ (fromIntegral (length xs))) (count2map' xs)
165 -- | insert in a dict
166 count2map' :: (Ord k, Foldable t) => t k -> Map k Double
167 count2map' xs = L.foldl' (\x y -> insertWith (+) y 1 x) M.empty xs
170 trunc :: (RealFrac a, Integral c, Integral b) => b -> a -> c
171 trunc n = truncate . (* 10^n)
173 trunc' :: Int -> Double -> Double
174 trunc' n x = fromIntegral $ truncate $ (x * 10^n)
177 bool2int :: Num a => Bool -> a
178 bool2int b = case b of
182 bool2double :: Bool -> Double
183 bool2double bool = case bool of
189 -- Normalizing && scaling data
190 scale :: [Double] -> [Double]
193 scaleMinMax :: [Double] -> [Double]
194 scaleMinMax xs = map (\x -> (x - mi / (ma - mi + 1) )) xs'
200 scaleNormalize :: [Double] -> [Double]
201 scaleNormalize xs = map (\x -> (x - v / (m + 1))) xs'
209 normalize :: [Double] -> [Double]
210 normalize as = normalizeWith identity as
212 normalizeWith :: Fractional b => (a -> b) -> [a] -> [b]
213 normalizeWith extract bs = map (\x -> x/(sum bs')) bs'
217 -- Zip functions to add
218 zipFst :: ([b] -> [a]) -> [b] -> [(a, b)]
219 zipFst f xs = zip (f xs) xs
221 zipSnd :: ([a] -> [b]) -> [a] -> [(a, b)]
222 zipSnd f xs = zip xs (f xs)
225 unMaybe :: [Maybe a] -> [a]
226 unMaybe = map fromJust . L.filter isJust