-
Notifications
You must be signed in to change notification settings - Fork 5
/
Utils.hs
55 lines (37 loc) · 1.47 KB
/
Utils.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{-# LANGUAGE NoMonomorphismRestriction #-}
module Utils where
import qualified Data.ByteString
import Control.Monad
import Data.Char
import Data.Maybe (fromMaybe)
import Data.Tuple (swap)
import qualified Data.Map as M
import Debug.Trace
import Test.HUnit
liftM3M f a b c = join $ liftM3 f a b c
reverseMap x = M.fromList $ map swap $ M.toList x
st testName expected actual = TestLabel testName $ TestCase $ assertEqual "" expected actual
simpleTests x = runTestTT $ TestList x
xtrace _ b = b
ztrace m t = trace (m ++ " = " ++ show t) t
xeqTrace _ _ y = y
zeqTrace m x y = if x == y then y else trace (m ++ ": " ++ show x ++ " /= " ++ show y) y
consMaybe Nothing value = value
consMaybe (Just x) value = x : value
fromRight v = case v of
Right r -> r
Left l -> error $ show l
constructJust cond val = if cond then Nothing else Just val
deconstructJust (Just j) _ = j
deconstructJust Nothing val = val
packL = Data.ByteString.pack . map (fromIntegral . ord)
joinStr _ [] = ""
joinStr sep l = foldl1 (\x y -> x ++ sep ++ y) l
inStrings l r x = l ++ x ++ r
-- TODO refactor Utils.showJoinedList and Utils.showJoinedList2 to extract
showJoinedList separator = joinStr separator . map show
showJoinedList2 separator = concatMap (\x -> show x ++ separator)
inParens = inStrings "(" ")"
inAngular = inStrings "<" ">"
uncondLookup = tracedUncondLookup "No trace"
tracedUncondLookup msg k m = fromMaybe (error $ msg ++ ": uncondLookup cannot find " ++ show k ++ " in " ++ show m) $ M.lookup k m