mirror of
https://github.com/c-cube/iter.git
synced 2025-12-06 03:05:29 -05:00
tests module
This commit is contained in:
parent
4982630967
commit
d29a21f4f5
1 changed files with 28 additions and 0 deletions
28
tests.ml
Normal file
28
tests.ml
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
|
||||
(** {2 Test sequences} *)
|
||||
|
||||
let seq_of_list l = Sequence.from_iter (fun k -> List.iter k l)
|
||||
|
||||
let list_of_seq seq = List.rev (Sequence.fold (fun y x -> x::y) [] seq)
|
||||
|
||||
let seq_of_hashtbl h =
|
||||
Sequence.from_iter (fun k -> Hashtbl.iter (fun a b -> k (a, b)) h)
|
||||
|
||||
let hashtbl_from_seq seq =
|
||||
let h = Hashtbl.create 3 in
|
||||
Sequence.iter (fun (k,v) -> Hashtbl.replace h k v) seq;
|
||||
h
|
||||
|
||||
(** print a list of items using the printing function *)
|
||||
let rec pp_list ?(sep=", ") pp_item formatter = function
|
||||
| x::y::xs -> Format.fprintf formatter "%a%s@,%a"
|
||||
pp_item x sep (pp_list ~sep:sep pp_item) (y::xs)
|
||||
| x::[] -> pp_item formatter x
|
||||
| [] -> ()
|
||||
|
||||
let _ =
|
||||
let l = [0;1;2;3;4;5;6] in
|
||||
let l' = list_of_seq (Sequence.filter (fun x -> x mod 2 = 0) (seq_of_list l)) in
|
||||
Format.printf "l=@[<h>[%a]@]; l'=@[<h>[%a]@]@."
|
||||
(pp_list Format.pp_print_int) l
|
||||
(pp_list Format.pp_print_int) l'
|
||||
Loading…
Add table
Reference in a new issue