From 2ee7f6a3d37cdc1ddc3427dd273eb441f715025a Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Tue, 3 Dec 2024 08:53:02 +0000 Subject: [PATCH] Rename functions to be more idiomatic. --- bin/day2401.ml | 10 +++++----- bin/day2402.ml | 7 +++---- bin/day2403.ml | 2 +- lib/aoc.ml | 4 ++-- lib/aoc.mli | 4 ++-- 5 files changed, 13 insertions(+), 14 deletions(-) diff --git a/bin/day2401.ml b/bin/day2401.ml index 0d51610..7a6e0b0 100644 --- a/bin/day2401.ml +++ b/bin/day2401.ml @@ -1,7 +1,7 @@ (** [pair_nums_from_string s] takes a string of two numbers separated by whitespace and returns the pair of the numbers *) -let pair_nums_from_string s = - match Aoc.nums_from_string s with +let pair_ints_of_string s = + match Aoc.ints_of_string s with | [ h; h' ] -> (h, h') | _ -> raise (Invalid_argument "pair_nums_from_string") @@ -25,8 +25,8 @@ let accumulate = List.fold_left ( + ) 0 (** [lists_from_file fname] Read two lists of integers from [fname] and return as a pair. *) -let lists_from_file fname = - Aoc.strings_from_file fname |> List.map pair_nums_from_string |> rev_split +let lists_of_file fname = + Aoc.strings_of_file fname |> List.map pair_ints_of_string |> rev_split let day2401a (a, b) = List.map2 Aoc.distance1 (List.sort Int.compare a) (List.sort Int.compare b) @@ -35,5 +35,5 @@ let day2401a (a, b) = let day2401b (a, b) = List.map (count b) a |> List.map2 ( * ) a |> accumulate let _ = - Aoc.main lists_from_file + Aoc.main lists_of_file [ (string_of_int, day2401a); (string_of_int, day2401b) ] diff --git a/bin/day2402.ml b/bin/day2402.ml index d4e76cd..d2ccb2b 100644 --- a/bin/day2402.ml +++ b/bin/day2402.ml @@ -15,12 +15,11 @@ let is_safe_dampened lst = in impl [] lst -let nums_from_file fname = - Aoc.strings_from_file fname |> List.map Aoc.nums_from_string +let ints_of_file fname = + Aoc.strings_of_file fname |> List.map Aoc.ints_of_string let day2402a lsts = List.filter is_safe lsts |> List.length let day2402b lsts = List.filter is_safe_dampened lsts |> List.length let _ = - Aoc.main nums_from_file - [ (string_of_int, day2402a); (string_of_int, day2402b) ] + Aoc.main ints_of_file [ (string_of_int, day2402a); (string_of_int, day2402b) ] diff --git a/bin/day2403.ml b/bin/day2403.ml index 3d8a96f..010736d 100644 --- a/bin/day2403.ml +++ b/bin/day2403.ml @@ -22,7 +22,7 @@ let instrs_of_string s = List.rev (impl [] 0) let instrs_of_file fname = - Aoc.strings_from_file fname |> List.map instrs_of_string |> List.concat + Aoc.strings_of_file fname |> List.map instrs_of_string |> List.concat (** [mac_opt acc a b] returns [acc + a' * b'] if [a = Some a'] and [b = Some b']. If either [a] or [b] are [None] then the result is [acc]. *) diff --git a/lib/aoc.ml b/lib/aoc.ml index a5b537e..1e87e34 100644 --- a/lib/aoc.ml +++ b/lib/aoc.ml @@ -1,7 +1,7 @@ -let nums_from_string s = List.map int_of_string (Str.split (Str.regexp " +") s) +let ints_of_string s = List.map int_of_string (Str.split (Str.regexp " +") s) let distance1 a b = abs (a - b) -let strings_from_file fname = +let strings_of_file fname = In_channel.with_open_text fname In_channel.input_lines let main prep parts = diff --git a/lib/aoc.mli b/lib/aoc.mli index d5b6fc4..04f2a41 100644 --- a/lib/aoc.mli +++ b/lib/aoc.mli @@ -1,11 +1,11 @@ -val nums_from_string : string -> int list +val ints_of_string : string -> int list (** [nums_from_string s] takes a string of space separated integers and gives back a list of the integers. *) val distance1 : int -> int -> int (** [distance1 a b] returns the absolute difference between [a] and [b]. *) -val strings_from_file : string -> string list +val strings_of_file : string -> string list (** [strings_from_file fname] returns a list of strings from the file [fname]. Each string represents a line from the file. *)