iterate on integers

This commit is contained in:
Simon Cruanes 2013-01-28 00:34:54 +01:00
parent bd7ecf0b96
commit 57b778f6cc
3 changed files with 17 additions and 0 deletions

View file

@ -66,3 +66,10 @@ module Hashtbl =
from_iter (fun k -> Hashtbl.iter (fun a b -> k (a, b)) h)
end
module Int =
struct
let range ~start ~stop =
let seq_fun k =
for i = start to stop do k i done
in { seq_fun; }
end

View file

@ -45,3 +45,11 @@ module Hashtbl :
val of_seq : ('a * 'b) t -> ('a, 'b) Hashtbl.t
val to_seq : ('a, 'b) Hashtbl.t -> ('a * 'b) t
end
(** Iterate on ranges of ints *)
module Int :
sig
val range : start:int -> stop:int -> int t
(** Iterator on [start...stop] by steps 1 *)
end

View file

@ -22,8 +22,10 @@ let _ =
(Sequence.map (fun (x, y) -> (string_of_int x) ^ " -> " ^ (string_of_int y))
(Sequence.Hashtbl.to_seq h))
in
let l3 = Sequence.List.of_seq (Sequence.Int.range ~start:0 ~stop:42) in
Format.printf "l=@[<h>[%a]@]@." (pp_list Format.pp_print_int) l;
Format.printf "l'=@[<h>[%a]@]@." (pp_list Format.pp_print_int) l';
Format.printf "l''=@[<h>[%a]@]@." (pp_list Format.pp_print_int) l'';
Format.printf "l2=@[<h>[%a]@]@." (pp_list Format.pp_print_string) l2;
Format.printf "l3=@[<h>[%a]@]@." (pp_list Format.pp_print_int) l3;
()