-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtils.hs
More file actions
30 lines (22 loc) · 708 Bytes
/
Utils.hs
File metadata and controls
30 lines (22 loc) · 708 Bytes
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
module Utils where
import Data.Maybe
import qualified Data.List as List
swap :: (a,b) -> (b,a)
swap (x,y) = (y,x)
fst3 :: (a,b,c) -> a
fst3 (x,_,_) = x
snd3 :: (a,b,c) -> b
snd3 (_,y,_) = y
thd3 :: (a,b,c) -> c
thd3 (_,_,z) = z
find :: (Eq a) => a -> [(a,b)] -> b
find a = fromJust . lookup a
normalize :: [(a,Float)] -> [(a,Float)]
normalize ps = [(a,p * z) | (a,p) <- ps]
where z = 1 / (sum $ map snd ps)
-- better name for this? ensures uniqueness of keys
-- probably a better implementation too
flatten :: Eq a => [(a,Float)] -> [(a,Float)]
flatten [] = []
flatten ps@((name,_):_) = (name, sum $ map snd vs) : flatten ps'
where (vs,ps') = List.partition ((== name) . fst) ps