From 57b778f6cca1f3b6775f7dcf878c6666d7d3ba15 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Mon, 28 Jan 2013 00:34:54 +0100 Subject: [PATCH] iterate on integers --- sequence.ml | 7 +++++++ sequence.mli | 8 ++++++++ tests.ml | 2 ++ 3 files changed, 17 insertions(+) diff --git a/sequence.ml b/sequence.ml index 8d5915b..58100c5 100644 --- a/sequence.ml +++ b/sequence.ml @@ -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 diff --git a/sequence.mli b/sequence.mli index 05585e6..b21562c 100644 --- a/sequence.mli +++ b/sequence.mli @@ -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 + diff --git a/tests.ml b/tests.ml index b205170..adae105 100644 --- a/tests.ml +++ b/tests.ml @@ -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=@[[%a]@]@." (pp_list Format.pp_print_int) l; Format.printf "l'=@[[%a]@]@." (pp_list Format.pp_print_int) l'; Format.printf "l''=@[[%a]@]@." (pp_list Format.pp_print_int) l''; Format.printf "l2=@[[%a]@]@." (pp_list Format.pp_print_string) l2; + Format.printf "l3=@[[%a]@]@." (pp_list Format.pp_print_int) l3; ()