-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.ml
59 lines (45 loc) · 1.93 KB
/
util.ml
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
let rec map f = function
| [] -> []
| x :: xs -> f x :: map f xs
let rec filter p = function
| [] -> []
| x :: xs -> if p x then x :: filter p xs else filter p xs
let rec notwhere p = function
| [] -> []
| x :: xs -> if p x then notwhere p xs else x :: notwhere p xs
let rec fold f a = function
| [] -> a
| x :: xs -> fold f (f a x) xs
let rec all p = function
| [] -> true
| x :: xs -> if p x then all p xs else false
let rec any p = function
| [] -> false
| x :: xs -> if p x then true else any p xs
let rec member = function
| [] -> fun _ -> false
| y :: ys -> fun x -> if x = y then true else member ys x
let rec find key = function
| [] -> None
| (k, v) :: vs -> if key = k then Some v else find key vs
let rec append xs = function
| [] -> Some xs
| (k, v) as y :: ys -> match find k xs with
| Some x -> if x = v then append xs ys else None
| None -> match append xs ys with
| Some zs -> Some (y :: zs)
| None -> None
let union xs ys = xs @ notwhere (member xs) ys
let subtraction xs ys = notwhere (member ys) xs
let intersection xs ys = filter (member ys) xs
let reverse xs = let rec inner = function
| [] -> fun r -> r
| x' :: xs' -> fun r -> inner xs' (x' :: r)
in inner xs []
let some = function
| Some _ -> true
| None -> false
let sgn = function
| n when n > 0 -> 1
| n when n < 0 -> -1
| _ -> 0