summaryrefslogtreecommitdiff
path: root/src/utils.ml
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.ml')
-rw-r--r--src/utils.ml32
1 files changed, 15 insertions, 17 deletions
diff --git a/src/utils.ml b/src/utils.ml
index 62ae3a5..833c972 100644
--- a/src/utils.ml
+++ b/src/utils.ml
@@ -1,33 +1,31 @@
1exception Empty_list 1exception Empty_list
2
2let fold fn = function 3let fold fn = function
3 | [] -> raise Empty_list 4 | [] -> raise Empty_list
4 | head :: tail -> List.fold_left fn head tail 5 | head :: tail -> List.fold_left fn head tail
5 6
6let list_min ls = fold Stdlib.min ls 7let list_min ls = fold Stdlib.min ls
8
7let list_max ls = fold Stdlib.max ls 9let list_max ls = fold Stdlib.max ls
8 10
9let rec repeat n a = 11let rec repeat n a = if n <= 0 then [] else a :: repeat (n - 1) a
10 if n <= 0
11 then []
12 else a :: repeat (n - 1) a
13 12
14let empty ls = (ls = []) 13let empty ls = ls = []
15 14
16let rec zip l1 l2 = 15let rec zip l1 l2 =
17 match (l1, l2) with 16 match (l1, l2) with
18 | ([], _) -> [] 17 | [], _ -> []
19 | (_, []) -> [] 18 | _, [] -> []
20 | (h1 :: t1, h2 :: t2) -> (h1, h2) :: zip t1 t2 19 | h1 :: t1, h2 :: t2 -> (h1, h2) :: zip t1 t2
21 20
22let rec transpose = function 21let rec transpose = function
23 | [] :: _ -> [] 22 | [] :: _ -> []
24 | ls -> List.(map hd ls) :: transpose List.(map tl ls) 23 | ls -> List.(map hd ls) :: transpose List.(map tl ls)
25 24
26let flip f a b = f b a 25let flip f a b = f b a
27 26
28let rot_right ls = List.(rev ls |> transpose) 27let rot_right ls = List.(rev ls |> transpose)
29let rot_left ls = List.(transpose ls |> rev)
30 28
31let saturating_sub b a = 29let rot_left ls = List.(transpose ls |> rev)
32 if a < b then 0 30
33 else a - b 31let saturating_sub b a = if a < b then 0 else a - b