From 0f34d9a931791406d2cabf282742530195f71927 Mon Sep 17 00:00:00 2001 From: DJ O'Leary Date: Mon, 8 Dec 2025 01:55:28 +0100 Subject: refactor(2025,ocaml): use result type for answers to allow for returning errors --- 2025/ocaml/lib/application/d02p02.ml | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to '2025/ocaml/lib/application/d02p02.ml') diff --git a/2025/ocaml/lib/application/d02p02.ml b/2025/ocaml/lib/application/d02p02.ml index 29e3877..d062f7b 100644 --- a/2025/ocaml/lib/application/d02p02.ml +++ b/2025/ocaml/lib/application/d02p02.ml @@ -1,5 +1,9 @@ open Domain.D02p02 +(* Errors *) + +let err_empty_input = "input is empty" + (* Logging *) (** [passthrough_with_logs logger lst] is [lst] with the side-effects caused by calling [logger] on [lst]. *) @@ -67,11 +71,16 @@ let is_invalid id = List.length repeating_patterns > 0 let solve input () = - input |> input_to_lines |> List.to_seq - |> passthrough_with_logs pp_lines - |> lines_to_ranges - |> passthrough_with_logs pp_ranges - |> collect_all - |> passthrough_with_logs pp_collection - |> Seq.map ID.to_string |> Seq.filter is_invalid - |> Seq.fold_left (fun acc id -> acc + int_of_string id) 0 + if input = "" then Error err_empty_input + else + let answer = + input |> input_to_lines |> List.to_seq + |> passthrough_with_logs pp_lines + |> lines_to_ranges + |> passthrough_with_logs pp_ranges + |> collect_all + |> passthrough_with_logs pp_collection + |> Seq.map ID.to_string |> Seq.filter is_invalid + |> Seq.fold_left (fun acc id -> acc + int_of_string id) 0 + in + Ok answer -- cgit v1.2.3