diff options
| author | DJ O'Leary <dijitol@proton.me> | 2025-12-11 03:52:59 +0100 |
|---|---|---|
| committer | DJ O'Leary <dijitol@proton.me> | 2025-12-11 03:52:59 +0100 |
| commit | a7c59b1dba9bc54d1283a8d242e95984b0a8e791 (patch) | |
| tree | 38b0502a20db5654a1931a1a7c27d7d2c5d57c37 /2025 | |
| parent | d08ec9d21b56ccad803b3b8937ad8aa6e64f9598 (diff) | |
refactor(matrix): add to_list and make pp prettier
Diffstat (limited to '2025')
| -rw-r--r-- | 2025/ocaml/lib/domain/matrix.ml | 31 |
1 files changed, 21 insertions, 10 deletions
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" |
