aoc2022/lib/utils.ml

36 lines
821 B
OCaml
Raw Permalink Normal View History

2022-12-01 23:45:12 +01:00
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
List.rev (aux [])
2022-12-01 23:45:12 +01:00
2022-12-03 17:01:56 +01:00
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
2022-12-04 09:32:12 +01:00
let pair_of_list l = (List.hd l, List.tl l |> List.hd)
let range x =
let rec aux n res =
if n = 0 then res
else aux (n - 1) (n :: res) in
aux x []
let id x = x