From a7c59b1dba9bc54d1283a8d242e95984b0a8e791 Mon Sep 17 00:00:00 2001 From: DJ O'Leary Date: Thu, 11 Dec 2025 03:52:59 +0100 Subject: refactor(matrix): add to_list and make pp prettier --- 2025/ocaml/lib/domain/matrix.ml | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to '2025') diff --git a/2025/ocaml/lib/domain/matrix.ml b/2025/ocaml/lib/domain/matrix.ml index 8983287..dadd60a 100644 --- a/2025/ocaml/lib/domain/matrix.ml +++ b/2025/ocaml/lib/domain/matrix.ml @@ -6,7 +6,12 @@ module M : sig where each element in the array is initialized to [initial]. *) val of_list : 'a list list -> 'a t - (** [of_list lst] is a 2D array where every element in the list is an element. *) + (** [of_list lst] is a 2D array where every element in the array is an + element corresponds to an element of the [lst]. *) + + val to_list : 'a t -> 'a list list + (** [to_list matrix] is a 2D list where every element in the list is an + element corresponds to an element of the [matrix]. *) val of_string : string -> string t (** [of_string str] is a 2D array where every character is an element. *) @@ -34,6 +39,8 @@ end = struct let of_list lst = Array.of_list @@ List.map Array.of_list lst + let to_list matrix = Array.to_list @@ Array.map Array.to_list matrix + let of_string str = let lines = str |> String.split_on_char '\n' @@ -72,15 +79,19 @@ end = struct potential_neighbours let pp string_of_elt matrix = - Array.fold_left - (fun col_acc arr -> - let row = - Array.fold_left - (fun row_acc elt -> row_acc ^ string_of_elt elt) - "" arr - in - if col_acc = "" then row else col_acc ^ "\n" ^ row ) - "" matrix + "[" + ^ Array.fold_left + (fun col_acc arr -> + let row = + "[" + ^ Array.fold_left + (fun row_acc elt -> row_acc ^ string_of_elt elt ^ "; ") + " " arr + ^ "]" + in + col_acc ^ " " ^ row ^ "\n" ) + "\n" matrix + ^ "]" end let%expect_test "matrix created from input is pretty-printed as the same matrix" -- cgit v1.2.3