From d4f3642b8dd21c53804cc30b535117697d9f036a Mon Sep 17 00:00:00 2001 From: "Aaron L. Zeng" Date: Fri, 29 Dec 2023 19:20:06 -0500 Subject: [PATCH] Use let%tydi to avoid awkward single-branch match expression --- 2019/05/main.ml | 18 +++++------ 2019/09/main.ml | 18 +++++------ 2019/17/main.ml | 23 ++++++-------- 2019/21/main.ml | 82 ++++++++++++++++++++++++------------------------- 4 files changed, 66 insertions(+), 75 deletions(-) diff --git a/2019/05/main.ml b/2019/05/main.ml index d3733d1..253cf0e 100644 --- a/2019/05/main.ml +++ b/2019/05/main.ml @@ -5,11 +5,10 @@ open Intcode let a () = let%bind program = Reader.file_contents "aoc.in" >>| Program.of_string in - match Program.Async.run program with - | { input; output; done_ } -> - Pipe.write_without_pushback input 1; - let%bind () = Pipe.iter_without_pushback output ~f:(printf "%d\n") in - done_ + let%tydi { input; output; done_ } = Program.Async.run program in + Pipe.write_without_pushback input 1; + let%bind () = Pipe.iter_without_pushback output ~f:(printf "%d\n") in + done_ ;; let%expect_test "a" = @@ -30,11 +29,10 @@ let%expect_test "a" = let b () = let%bind program = Reader.file_contents "aoc.in" >>| Program.of_string in - match Program.Async.run program with - | { input; output; done_ } -> - Pipe.write_without_pushback input 5; - let%bind () = Pipe.iter_without_pushback output ~f:(printf "%d\n") in - done_ + let%tydi { input; output; done_ } = Program.Async.run program in + Pipe.write_without_pushback input 5; + let%bind () = Pipe.iter_without_pushback output ~f:(printf "%d\n") in + done_ ;; let%expect_test "b" = diff --git a/2019/09/main.ml b/2019/09/main.ml index 9d337ca..e251e95 100644 --- a/2019/09/main.ml +++ b/2019/09/main.ml @@ -7,11 +7,10 @@ let input () = Reader.file_contents "aoc.in" >>| Program.of_string let a () = let%bind program = input () in - match Program.Async.run program with - | { input; output; done_ } -> - Pipe.write_without_pushback input 1; - let%bind () = Pipe.iter_without_pushback output ~f:(printf "%d\n") in - done_ + let%tydi { input; output; done_ } = Program.Async.run program in + Pipe.write_without_pushback input 1; + let%bind () = Pipe.iter_without_pushback output ~f:(printf "%d\n") in + done_ ;; let%expect_test "a" = @@ -22,11 +21,10 @@ let%expect_test "a" = let b () = let%bind program = input () in - match Program.Async.run program with - | { input; output; done_ } -> - Pipe.write_without_pushback input 2; - let%bind () = Pipe.iter_without_pushback output ~f:(printf "%d\n") in - done_ + let%tydi { input; output; done_ } = Program.Async.run program in + Pipe.write_without_pushback input 2; + let%bind () = Pipe.iter_without_pushback output ~f:(printf "%d\n") in + done_ ;; let%expect_test "b" = diff --git a/2019/17/main.ml b/2019/17/main.ml index cfc1530..221dbf0 100644 --- a/2019/17/main.ml +++ b/2019/17/main.ml @@ -8,19 +8,16 @@ let debug = false let input () = Reader.file_contents "aoc.in" >>| Program.of_string let output program = - (* FIXME: Replace match statements like these with the equivalent let-binding when - type-directed disambiguation works therefor. *) - match Program.Async.run program with - | { input; output; done_ } -> - let buffer = Buffer.create 0 in - Pipe.close input; - let%bind () = - Pipe.iter_without_pushback output ~f:(fun c -> - Buffer.add_char buffer (Char.of_int_exn c)) - and () = done_ in - (* Need the String.strip because there is an empty line at the end of the - output. *) - return (Buffer.contents buffer |> String.strip |> String.split_lines |> Array.of_list) + let%tydi { input; output; done_ } = Program.Async.run program in + let buffer = Buffer.create 0 in + Pipe.close input; + let%bind () = + Pipe.iter_without_pushback output ~f:(fun c -> + Buffer.add_char buffer (Char.of_int_exn c)) + and () = done_ in + (* Need the String.strip because there is an empty line at the end of the + output. *) + return (Buffer.contents buffer |> String.strip |> String.split_lines |> Array.of_list) ;; let map = diff --git a/2019/21/main.ml b/2019/21/main.ml index ab08991..e11314c 100644 --- a/2019/21/main.ml +++ b/2019/21/main.ml @@ -7,24 +7,23 @@ let input () = Reader.file_contents "aoc.in" >>| Program.of_string let a () = let%bind program = input () in - match Program.Async.run program with - | { input; output; done_ } -> - let print_line line = - String.iter (line ^ "\n") ~f:(fun c -> - Pipe.write_without_pushback input (Char.to_int c)) - in - print_line "NOT A J"; - print_line "NOT B T"; - print_line "OR T J"; - print_line "NOT C T"; - print_line "OR T J"; - print_line "AND D J"; - print_line "WALK"; - let%bind () = - Pipe.iter_without_pushback output ~f:(fun c -> - if c > 255 then printf "%d\n" c else print_char (Char.of_int_exn c)) - in - done_ + let%tydi { input; output; done_ } = Program.Async.run program in + let print_line line = + String.iter (line ^ "\n") ~f:(fun c -> + Pipe.write_without_pushback input (Char.to_int c)) + in + print_line "NOT A J"; + print_line "NOT B T"; + print_line "OR T J"; + print_line "NOT C T"; + print_line "OR T J"; + print_line "AND D J"; + print_line "WALK"; + let%bind () = + Pipe.iter_without_pushback output ~f:(fun c -> + if c > 255 then printf "%d\n" c else print_char (Char.of_int_exn c)) + in + done_ ;; let%expect_test "a" = @@ -75,30 +74,29 @@ let%expect_test "a" = let b () = let%bind program = input () in - match Program.Async.run program with - | { input; output; done_ } -> - let print_line line = - String.iter (line ^ "\n") ~f:(fun c -> - Pipe.write_without_pushback input (Char.to_int c)) - in - print_line "NOT J J"; - print_line "AND D J"; - print_line "OR A T"; - print_line "AND B T"; - print_line "AND C T"; - print_line "AND D T"; - print_line "NOT T T"; - print_line "AND T J"; - print_line "AND E T"; - print_line "OR E T"; - print_line "OR H T"; - print_line "AND T J"; - print_line "RUN"; - let%bind () = - Pipe.iter_without_pushback output ~f:(fun c -> - if c > 255 then printf "%d\n" c else print_char (Char.of_int_exn c)) - in - done_ + let%tydi { input; output; done_ } = Program.Async.run program in + let print_line line = + String.iter (line ^ "\n") ~f:(fun c -> + Pipe.write_without_pushback input (Char.to_int c)) + in + print_line "NOT J J"; + print_line "AND D J"; + print_line "OR A T"; + print_line "AND B T"; + print_line "AND C T"; + print_line "AND D T"; + print_line "NOT T T"; + print_line "AND T J"; + print_line "AND E T"; + print_line "OR E T"; + print_line "OR H T"; + print_line "AND T J"; + print_line "RUN"; + let%bind () = + Pipe.iter_without_pushback output ~f:(fun c -> + if c > 255 then printf "%d\n" c else print_char (Char.of_int_exn c)) + in + done_ ;; let%expect_test "b" =