2 Module : Gargantext.Utils
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@.
15 module Gargantext.Core.Utils (
16 -- module Gargantext.Utils.Chronos
17 module Gargantext.Core.Utils.Prefix
26 import Data.Char (chr, ord)
27 import qualified Data.List as List
30 import Data.Text (Text, pack)
32 import System.Random (initStdGen, uniformR)
34 -- import Gargantext.Utils.Chronos
35 import Gargantext.Core.Utils.Prefix
36 import Gargantext.Prelude
39 something :: Monoid a => Maybe a -> a
40 something Nothing = mempty
41 something (Just a) = a
44 alphanum = (chr <$> digits) <> (chr <$> lowercase) <> (chr <$> uppercase)
46 digits = [(ord '0')..(ord '9')]
47 lowercase = [(ord 'a')..(ord 'z')]
48 uppercase = [(ord 'A')..(ord 'Z')]
50 choices :: Int -> [a] -> IO [a]
54 let (cIdx, _) = uniformR (0, length lst - 1) gen
56 choices' <- choices (num - 1) lst
59 randomString :: Int -> IO Text
61 str <- choices num alphanum
65 -- | Given a list of items of type 'a', return list with unique items
66 -- (like List.nub) but tuple-d with their counts in the original list
67 groupWithCounts :: (Ord a, Eq a) => [a] -> [(a, Int)]
68 groupWithCounts = map f
72 f [] = panic "[groupWithCounts] impossible"
73 f ts@(t:_) = (t, length ts)
75 addTuples :: (Num a, Num b) => (a, b) -> (a, b) -> (a, b)
76 addTuples (a1, b1) (a2, b2) = (a1 + a2, b1 + b2)