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)
36 , Ord, Integral, Foldable, RealFrac, Monad, filter
37 , reverse, map, zip, drop, take, zipWith
38 , sum, fromIntegral, length, fmap, foldl, foldl'
39 , takeWhile, sqrt, undefined, identity
40 , abs, min, max, maximum, minimum, return, snd, truncate
41 , (+), (*), (/), (-), (.), ($), (**), (^), (<), (>), log
42 , Eq, (==), (>=), (<=), (<>), (/=)
45 , elem, die, mod, div, const
50 -- TODO import functions optimized in Utils.Count
51 -- import Protolude hiding (head, last, all, any, sum, product, length)
52 -- import Gargantext.Utils.Count
53 import qualified Data.List as L hiding (head, sum)
54 import qualified Control.Monad as M
57 import qualified Data.Map as M
59 import Data.Map.Strict (insertWith)
60 import qualified Data.Vector as V
62 import Text.Show (Show(), show)
63 import Text.Read (Read())
64 import Data.String.Conversions (cs)
66 --pf :: (a -> Bool) -> [a] -> [a]
72 --pm :: (a -> b) -> [a] -> [b]
75 map2 :: (t -> b) -> [[t]] -> [[b]]
76 map2 fun = map (map fun)
78 pz :: [a] -> [b] -> [(a, b)]
81 pd :: Int -> [a] -> [a]
84 ptk :: Int -> [a] -> [a]
87 pzw :: (a -> b -> c) -> [a] -> [b] -> [c]
90 -- Exponential Average
91 eavg :: [Double] -> Double
92 eavg (x:xs) = a*x + (1-a)*(eavg xs)
97 mean :: Fractional a => [a] -> a
98 mean xs = if L.null xs then 0.0
99 else sum xs / fromIntegral (length xs)
101 sumMaybe :: Num a => [Maybe a] -> Maybe a
102 sumMaybe = fmap sum . M.sequence
104 variance :: Floating a => [a] -> a
105 variance xs = mean $ map (\x -> (x - m) ** 2) xs where
108 deviation :: [Double] -> Double
109 deviation = sqrt . variance
111 movingAverage :: Fractional b => Int -> [b] -> [b]
112 movingAverage steps xs = map mean $ chunkAlong steps 1 xs
114 ma :: [Double] -> [Double]
118 -- | Function to split a range into chunks
119 chunkAlong :: Int -> Int -> [a] -> [[a]]
120 chunkAlong a b l = only (while dropAlong)
123 while = takeWhile (\x -> length x >= a)
124 dropAlong = L.scanl (\x _y -> drop b x) l ([1..] :: [Integer])
126 -- | Optimized version (Vector)
127 chunkAlong' :: Int -> Int -> V.Vector a -> V.Vector (V.Vector a)
128 chunkAlong' a b l = only (while dropAlong)
130 only = V.map (V.take a)
131 while = V.takeWhile (\x -> V.length x >= a)
132 dropAlong = V.scanl (\x _y -> V.drop b x) l (V.fromList [1..])
134 -- | TODO Inverse of chunk ? unchunkAlong ?
135 unchunkAlong :: Int -> Int -> [[a]] -> [a]
136 unchunkAlong = undefined
139 -- splitAlong [2,3,4] ("helloworld" :: [Char]) == ["he", "llo", "worl", "d"]
140 splitAlong :: [Int] -> [Char] -> [[Char]]
141 splitAlong _ [] = [] -- No list? done
142 splitAlong [] xs = [xs] -- No place to split at? Return the remainder
143 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
145 takeWhileM :: (Monad m) => (a -> Bool) -> [m a] -> m [a]
146 takeWhileM _ [] = return []
147 takeWhileM p (a:as) = do
151 vs <- takeWhileM p as
156 -- To select the right algorithme according to the type:
157 -- https://github.com/mikeizbicki/ifcxt
159 sumSimple :: Num a => [a] -> a
160 sumSimple = L.foldl' (+) 0
162 -- | https://en.wikipedia.org/wiki/Kahan_summation_algorithm
163 sumKahan :: Num a => [a] -> a
164 sumKahan = snd . L.foldl' go (0,0)
166 go (c,t) i = ((t'-t)-y,t')
171 -- | compute part of the dict
172 count2map :: (Ord k, Foldable t) => t k -> Map k Double
173 count2map xs = M.map (/ (fromIntegral (length xs))) (count2map' xs)
175 -- | insert in a dict
176 count2map' :: (Ord k, Foldable t) => t k -> Map k Double
177 count2map' xs = L.foldl' (\x y -> insertWith (+) y 1 x) M.empty xs
180 trunc :: (RealFrac a, Integral c, Integral b) => b -> a -> c
181 trunc n = truncate . (* 10^n)
183 trunc' :: Int -> Double -> Double
184 trunc' n x = fromIntegral $ truncate $ (x * 10^n)
187 bool2int :: Num a => Bool -> a
188 bool2int b = case b of
192 bool2double :: Bool -> Double
193 bool2double bool = case bool of
199 -- Normalizing && scaling data
200 scale :: [Double] -> [Double]
203 scaleMinMax :: [Double] -> [Double]
204 scaleMinMax xs = map (\x -> (x - mi / (ma - mi + 1) )) xs'
210 scaleNormalize :: [Double] -> [Double]
211 scaleNormalize xs = map (\x -> (x - v / (m + 1))) xs'
219 normalize :: [Double] -> [Double]
220 normalize as = normalizeWith identity as
222 normalizeWith :: Fractional b => (a -> b) -> [a] -> [b]
223 normalizeWith extract bs = map (\x -> x/(sum bs')) bs'
227 -- Zip functions to add
228 zipFst :: ([b] -> [a]) -> [b] -> [(a, b)]
229 zipFst f xs = zip (f xs) xs
231 zipSnd :: ([a] -> [b]) -> [a] -> [(a, b)]
232 zipSnd f xs = zip xs (f xs)
236 unMaybe :: [Maybe a] -> [a]
237 unMaybe = map fromJust . L.filter isJust
239 -- | Syntactic convention for the reader/writer coordination.
240 -- @Motivation@: explicit functional flux ease coordination between
241 -- readers and writers who are not always the same individuals. Each
242 -- natural languages has its own syntaxical conventions from left to
243 -- right or the contrary. In computer programming languages it depends
244 -- on context of the algorithm itself and we need some clarity since
245 -- both are possible, here is a proposition to get more explicitiness.
247 -- | (<|) is called : "Pipe rightLeft" as "from right to left". The most right
248 -- function sends its output to the most left function which takes it as
250 (<|) :: (a -> b) -> a -> b
253 -- | (|>) is called : "Pipe leftRight" as "from left to right". The most left
254 -- function sends its output to the most right function which takes it as
255 -- input. (|>) == (&) = True -- in base prelude
256 (|>) :: a -> (a -> c) -> c
259 -- | Function composition orientation
260 (<.) :: (b -> c) -> (a -> b) -> a -> c
263 -- | Function composition orientation
264 (.>) :: (a -> b) -> (b -> c) -> a -> c