diff --git a/lib/server/import.ml b/lib/server/import.ml index 182dab4..4bd8601 100644 --- a/lib/server/import.ml +++ b/lib/server/import.ml @@ -30,6 +30,14 @@ module List = struct [@@tail_mod_cons] in iter xs + + let iter_up_to f xs ~limit = + let rec iter n = function + | x :: xs when n > 0 -> + f x; iter (n - 1) xs + | _ -> () + in + iter limit xs end include (val Logging.logs "Irc") diff --git a/lib/server/test_list.ml b/lib/server/test_list.ml index 59368c3..37285e3 100644 --- a/lib/server/test_list.ml +++ b/lib/server/test_list.ml @@ -12,6 +12,7 @@ let%expect_test _ = iter "[" "[]" xs in + let print_int_sp x = print_int x; print_string " " in let print_ints_nl xs = print_list print_int xs; print_newline () in print_ints_nl @@ -23,3 +24,8 @@ let%expect_test _ = [1;2;3;1;2]); [%expect {| [1;2;3;4;5;6;7;8;9;1;2;3;4;5;6] |}]; + List.iter_up_to ~limit:5 + print_int_sp + [1;2;3;4;5;6;7;8;9]; + print_newline (); + [%expect {| 1 2 3 4 5 |}];