Skip to content

Commit

Permalink
Use let%tydi to avoid awkward single-branch match expression
Browse files Browse the repository at this point in the history
  • Loading branch information
bcc32 committed Dec 30, 2023
1 parent a74ea44 commit d4f3642
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 75 deletions.
18 changes: 8 additions & 10 deletions 2019/05/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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" =
Expand All @@ -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" =
Expand Down
18 changes: 8 additions & 10 deletions 2019/09/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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" =
Expand All @@ -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" =
Expand Down
23 changes: 10 additions & 13 deletions 2019/17/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
82 changes: 40 additions & 42 deletions 2019/21/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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" =
Expand Down Expand Up @@ -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" =
Expand Down

0 comments on commit d4f3642

Please sign in to comment.