aoc2022/lib/utils.ml

27 lines
679 B
OCaml

let read_file name =
let ic = open_in name in
let try_read () =
try Some (input_line ic)
with End_of_file -> None in
let rec aux acc =
match try_read () with
| Some s -> aux (s::acc)
| None -> close_in ic; acc in
aux []
module CS = Set.Make(Char);;
let set_of_string str =
let rec aux s i st =
if i < 0 then st
else aux s (i - 1) (CS.add s.[i] st) in
aux str ((String.length str) - 1) CS.empty
let map_pair f (a, b) = (f a, f b)
let curry f x y = f (x, y)
let uncurry f (x, y) = f x y
let uncurry3 f (x, y, z) = f x y z
let swap x y = y x
let pair_of_list l = (List.hd l, List.tl l |> List.hd)