diff options
Diffstat (limited to 'src/utils.ml')
-rw-r--r-- | src/utils.ml | 32 |
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 @@ | |||
1 | exception Empty_list | 1 | exception Empty_list |
2 | |||
2 | let 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 |
5 | 6 | ||
6 | let list_min ls = fold Stdlib.min ls | 7 | let list_min ls = fold Stdlib.min ls |
8 | |||
7 | let list_max ls = fold Stdlib.max ls | 9 | let list_max ls = fold Stdlib.max ls |
8 | 10 | ||
9 | let rec repeat n a = | 11 | let 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 | ||
14 | let empty ls = (ls = []) | 13 | let empty ls = ls = [] |
15 | 14 | ||
16 | let rec zip l1 l2 = | 15 | let 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 | ||
22 | let rec transpose = function | 21 | let 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 | ||
26 | let flip f a b = f b a | 25 | let flip f a b = f b a |
27 | 26 | ||
28 | let rot_right ls = List.(rev ls |> transpose) | 27 | let rot_right ls = List.(rev ls |> transpose) |
29 | let rot_left ls = List.(transpose ls |> rev) | ||
30 | 28 | ||
31 | let saturating_sub b a = | 29 | let rot_left ls = List.(transpose ls |> rev) |
32 | if a < b then 0 | 30 | |
33 | else a - b | 31 | let saturating_sub b a = if a < b then 0 else a - b |