add List.iter_up_to
This commit is contained in:
parent
fde89493b2
commit
4e8381ea35
|
@ -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")
|
||||||
|
|
|
@ -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 |}];
|
||||||
|
|
Loading…
Reference in New Issue