-
Notifications
You must be signed in to change notification settings - Fork 2
/
Util.hs
50 lines (39 loc) · 1.34 KB
/
Util.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
{-# LANGUAGE TupleSections #-}
module Util where
import Control.Arrow
import Data.Array.IArray
import Data.Function
import Data.List
import Data.Maybe
import Data.Ord
fi :: (Integral a, Num b) => a -> b
fi = fromIntegral
fj :: Maybe a -> a
fj = fromJust
both :: (a -> b) -> (a,a) -> (b,b)
both f (x,y) = (f x, f y)
sortGroupOn :: Ord b => (a -> b) -> [a] -> [(b,[a])]
sortGroupOn f = sortOn f >>> groupBy ((==) `on` f) >>> map ((f.head) &&& id)
pairs :: [a] -> [(a,a)]
pairs [] = []
pairs (a:as) = map (a,) as ++ pairs as
withPairs :: Monoid r => (a -> a -> r) -> [a] -> r
withPairs _ [] = mempty
withPairs _ [_] = mempty
withPairs f (a:as) = go as
where
go [] = withPairs f as
go (a2:rest) = f a a2 <> go rest
-- Discrete binary search. Find the smallest integer in [lo,hi] such
-- that monotone predicate p holds.
binarySearchD :: Int -> Int -> (Int -> Bool) -> Int
binarySearchD lo hi p
| lo == hi = lo
| p mid = binarySearchD lo mid p
| otherwise = binarySearchD (mid+1) hi p
where
mid = (lo + hi) `div` 2
generate :: (Ix i, IArray a e) => (i,i) -> (i -> e) -> a i e
generate rng f = listArray rng (map f (range rng))
arraydef :: (Ix i, IArray a e) => (i,i) -> e -> [(i,e)] -> a i e
arraydef rng def vs = array rng ([(i,def) | i <- range rng] ++ vs)