summaryrefslogtreecommitdiff
path: root/src/utils.ml
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-08-08 13:08:33 +0100
committerAkshay <[email protected]>2021-08-08 13:08:33 +0100
commitc9e8dff919c49a13bff601e78cc1e78a7cc8f506 (patch)
treeb9015effe9dc995b083658b12b0de17d61230364 /src/utils.ml
parented2a97502cb825877562c05881da4fb2e8eecad6 (diff)
dump
Diffstat (limited to 'src/utils.ml')
-rw-r--r--src/utils.ml53
1 files changed, 25 insertions, 28 deletions
diff --git a/src/utils.ml b/src/utils.ml
index 8d1094e..62ae3a5 100644
--- a/src/utils.ml
+++ b/src/utils.ml
@@ -1,36 +1,33 @@
1module Utils = struct 1exception Empty_list
2 exception Empty_list 2let fold fn = function
3 let 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
6 5
7 let list_min ls = fold Stdlib.min ls 6let list_min ls = fold Stdlib.min ls
8 let list_max ls = fold Stdlib.max ls 7let list_max ls = fold Stdlib.max ls
9 8
10 let rec repeat n a = 9let rec repeat n a =
11 if n <= 0 10 if n <= 0
12 then [] 11 then []
13 else a :: repeat (n - 1) a 12 else a :: repeat (n - 1) a
14 13
15 let empty ls = (ls = []) 14let empty ls = (ls = [])
16 15
17 let rec zip l1 l2 = 16let rec zip l1 l2 =
18 match (l1, l2) with 17 match (l1, l2) with
19 | ([], _) -> [] 18 | ([], _) -> []
20 | (_, []) -> [] 19 | (_, []) -> []
21 | (h1 :: t1, h2 :: t2) -> (h1, h2) :: zip t1 t2 20 | (h1 :: t1, h2 :: t2) -> (h1, h2) :: zip t1 t2
22 21
23 let rec transpose = function 22let rec transpose = function
24 | [] :: _ -> [] 23 | [] :: _ -> []
25 | ls -> List.(map hd ls) :: transpose List.(map tl ls) 24 | ls -> List.(map hd ls) :: transpose List.(map tl ls)
26 25
27 let flip f a b = f b a 26let flip f a b = f b a
28 27
29 let rot_right ls = List.(rev ls |> transpose) 28let rot_right ls = List.(rev ls |> transpose)
30 let rot_left ls = List.(transpose ls |> rev) 29let rot_left ls = List.(transpose ls |> rev)
31
32 let saturating_sub b a =
33 if a < b then 0
34 else a - b
35end
36 30
31let saturating_sub b a =
32 if a < b then 0
33 else a - b