add List.iter_up_to

This commit is contained in:
tali 2024-01-31 11:27:27 -05:00
parent fde89493b2
commit 4e8381ea35
2 changed files with 14 additions and 0 deletions

View File

@ -30,6 +30,14 @@ module List = struct
[@@tail_mod_cons] [@@tail_mod_cons]
in in
iter xs 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 end
include (val Logging.logs "Irc") include (val Logging.logs "Irc")

View File

@ -12,6 +12,7 @@ let%expect_test _ =
iter "[" "[]" xs iter "[" "[]" xs
in 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 let print_ints_nl xs = print_list print_int xs; print_newline () in
print_ints_nl print_ints_nl
@ -23,3 +24,8 @@ let%expect_test _ =
[1;2;3;1;2]); [1;2;3;1;2]);
[%expect {| [1;2;3;4;5;6;7;8;9;1;2;3;4;5;6] |}]; [%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 |}];