-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUtils.hs
87 lines (59 loc) · 2.01 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
{-# LANGUAGE RebindableSyntax, NoImplicitPrelude #-}
module Utils where
import NumericPrelude
import Data.List hiding (product)
import Data.Array.MArray
import qualified Data.Map as Map
xor True = not
xor False = id
(f `on` g) x y = f (g x) (g y)
(f .-. g) x y = f $ g x y
-----------------------------------
-- some functions for printing ----
-----------------------------------
-- cim = concat-intersperse-map
cim t f l = concat $ intersperse t $ map f l
rshow s = if elem '+' ss then "(" ++ ss ++ ")" else ss
where ss = show s
-- for printing latex numbers with {}
texShow s = if length ss > 1 then "{" ++ ss ++ "}" else ss
where ss = show s
safeHead [] = Nothing
safeHead (x:xs) = Just x
safeTail [] = Nothing
safeTail (x:xs) = Just xs
safeFromJust str Nothing = error str
safeFromJust _ (Just x) = x
maybeIf True x = Just x
maybeIf False _ = Nothing
maybeZero Nothing = zero
maybeZero (Just x) = x
findM pred [] = return Nothing
findM pred (x:xs) = (pred x) >>= (\b -> if b then return $ Just x else findM pred xs)
fact :: Int -> Integer
fact n = product [1..fromIntegral n]
choose 0 0 = 1
choose 0 k = 0
choose n k
| k > n || n < 0 || k < 0 = 0
choose n k = (fact n) `div` ((fact k) * (fact $ n - k))
powerOf :: Int -> Int -> Bool
powerOf _ 0 = False
powerOf n i = n^(round $ logBase (fromIntegral n :: Double) (fromIntegral i :: Double)) == i
dropLast n = reverse . (drop n) . reverse
splitSubstr str ls = sst [] ls
where sst fst [] = (reverse fst,[])
sst fst rst = if take n rst == str
then (reverse fst,drop n rst)
else sst ((head rst):fst) (tail rst)
n = length str
class UnShow u where
unShow :: String -> u
evens [] = []
evens [x] = [x]
evens (x:_:xs) = x:(evens xs)
odds [] = []
odds x = evens $ tail x
modifyArray arr i fun = readArray arr i >>= (\x -> writeArray arr i (fun x))
partitionsBy valToKey list = map snd $ Map.toList
$ foldr (\elt mp -> Map.insertWith (++) (valToKey elt) [elt] mp) Map.empty list