summaryrefslogtreecommitdiff
path: root/src/utils.ml
blob: 8d1094ea830fba5a12264e7933a341bc5636841b (plain)
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
module Utils = struct
    exception Empty_list
    let fold fn = function
        | [] -> raise Empty_list
        | head :: tail -> List.fold_left fn head tail

    let list_min ls = fold Stdlib.min ls
    let list_max ls = fold Stdlib.max ls

    let rec repeat n a =
        if n <= 0
        then []
        else a :: repeat (n - 1) a

    let empty ls = (ls = [])

    let rec zip l1 l2 =
        match (l1, l2) with
        | ([], _) -> []
        | (_, []) -> []
        | (h1 :: t1, h2 :: t2) -> (h1, h2) :: zip t1 t2

    let rec transpose = function
        | [] :: _ -> []
        | ls -> List.(map hd ls) :: transpose List.(map tl ls)

    let flip f a b = f b a

    let rot_right ls = List.(rev ls |> transpose)
    let rot_left ls  = List.(transpose ls |> rev)

    let saturating_sub b a =
        if a < b then 0
        else a - b
end