mirror of
https://github.com/c-cube/iter.git
synced 2025-12-06 11:15:32 -05:00
merge from master
This commit is contained in:
commit
a50f307801
3 changed files with 19 additions and 5 deletions
11
sequence.ml
11
sequence.ml
|
|
@ -336,9 +336,9 @@ let take n seq k =
|
||||||
let count = ref 0 in
|
let count = ref 0 in
|
||||||
try
|
try
|
||||||
seq (fun x ->
|
seq (fun x ->
|
||||||
|
if !count = n then raise ExitSequence;
|
||||||
incr count;
|
incr count;
|
||||||
k x;
|
k x;
|
||||||
if !count = n then raise ExitSequence
|
|
||||||
)
|
)
|
||||||
with ExitSequence -> ()
|
with ExitSequence -> ()
|
||||||
|
|
||||||
|
|
@ -436,14 +436,17 @@ let to_list seq = List.rev (fold (fun y x -> x::y) [] seq)
|
||||||
|
|
||||||
let to_rev_list seq = fold (fun y x -> x :: y) [] seq
|
let to_rev_list seq = fold (fun y x -> x :: y) [] seq
|
||||||
|
|
||||||
|
let of_list l k = List.iter k l
|
||||||
|
|
||||||
|
let on_list f l =
|
||||||
|
to_list (f (of_list l))
|
||||||
|
|
||||||
let to_opt = head
|
let to_opt = head
|
||||||
|
|
||||||
let of_opt o k = match o with
|
let of_opt o k = match o with
|
||||||
| None -> ()
|
| None -> ()
|
||||||
| Some x -> k x
|
| Some x -> k x
|
||||||
|
|
||||||
let of_list l k = List.iter k l
|
|
||||||
|
|
||||||
let to_array seq =
|
let to_array seq =
|
||||||
let l = MList.of_seq seq in
|
let l = MList.of_seq seq in
|
||||||
let n = MList.length l in
|
let n = MList.length l in
|
||||||
|
|
@ -767,5 +770,3 @@ module IO = struct
|
||||||
let write_lines ?mode ?flags filename seq =
|
let write_lines ?mode ?flags filename seq =
|
||||||
write_to ?mode ?flags filename (snoc (intersperse "\n" seq) "\n")
|
write_to ?mode ?flags filename (snoc (intersperse "\n" seq) "\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -319,6 +319,11 @@ val to_rev_list : 'a t -> 'a list
|
||||||
|
|
||||||
val of_list : 'a list -> 'a t
|
val of_list : 'a list -> 'a t
|
||||||
|
|
||||||
|
val on_list : ('a t -> 'b t) -> 'a list -> 'b list
|
||||||
|
(** [on_list f l] is equivalent to [to_list @@ f @@ of_list l].
|
||||||
|
@since NEXT_RELEASE
|
||||||
|
*)
|
||||||
|
|
||||||
val to_opt : 'a t -> 'a option
|
val to_opt : 'a t -> 'a option
|
||||||
(** Alias to {!head}
|
(** Alias to {!head}
|
||||||
@since 0.5.1 *)
|
@since 0.5.1 *)
|
||||||
|
|
|
||||||
|
|
@ -190,6 +190,13 @@ let test_int_range () =
|
||||||
OUnit.assert_equal ~printer:pp_ilist [] S.(to_list (10 --^ 60));
|
OUnit.assert_equal ~printer:pp_ilist [] S.(to_list (10 --^ 60));
|
||||||
()
|
()
|
||||||
|
|
||||||
|
let test_take () =
|
||||||
|
let l = S.(to_list (take 0 (of_list [1]))) in
|
||||||
|
OUnit.assert_equal ~printer:pp_ilist [] l;
|
||||||
|
let l = S.(to_list (take 5 (of_list [1;2;3;4;5;6;7;8;9;10]))) in
|
||||||
|
OUnit.assert_equal ~printer:pp_ilist [1;2;3;4;5] l;
|
||||||
|
()
|
||||||
|
|
||||||
let suite =
|
let suite =
|
||||||
"test_sequence" >:::
|
"test_sequence" >:::
|
||||||
[ "test_empty" >:: test_empty;
|
[ "test_empty" >:: test_empty;
|
||||||
|
|
@ -217,4 +224,5 @@ let suite =
|
||||||
"test_unfoldr" >:: test_unfoldr;
|
"test_unfoldr" >:: test_unfoldr;
|
||||||
"test_hashtbl" >:: test_hashtbl;
|
"test_hashtbl" >:: test_hashtbl;
|
||||||
"test_int_range" >:: test_int_range;
|
"test_int_range" >:: test_int_range;
|
||||||
|
"test_take" >:: test_take;
|
||||||
]
|
]
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue