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