never executed always true always false
1 {- |
2 Module : NITTA.Utils.Base
3 Description : Utils for external libraries
4 Copyright : (c) Aleksandr Penskoi, 2020
5 License : BSD3
6 Maintainer : aleksandr.penskoi@gmail.com
7 Stability : experimental
8 -}
9 module NITTA.Utils.Base (
10 unionsMap,
11 oneOf,
12 minimumOn,
13 maximumOn,
14 toText,
15 fromText,
16 showText,
17 readText,
18 vsToStringList,
19 catchToMaybeIO,
20 ) where
21
22 import Control.Exception
23 import Data.Functor
24 import Data.List (maximumBy, minimumBy)
25 import Data.Set (elems, unions)
26 import Data.String
27 import Data.String.ToString
28 import Data.Text qualified as T
29 import System.Log.Logger (warningM)
30
31 unionsMap f lst = unions $ map f lst
32
33 oneOf = head . elems
34
35 minimumOn f = minimumBy (\a b -> f a `compare` f b)
36
37 maximumOn f = maximumBy (\a b -> f a `compare` f b)
38
39 toText v = T.pack $ toString v
40
41 fromText v = fromString $ T.unpack v
42
43 readText t = read $ T.unpack t
44
45 showText v = T.pack $ show v
46
47 vsToStringList vs = map toString $ elems vs
48
49 catchToMaybeIO action =
50 catch
51 (action <&> Just)
52 ( \(e :: IOException) -> do
53 warningM "NITTA" ("IO Exception: " <> show e)
54 return Nothing
55 )