add int_range_by

This commit is contained in:
Simon Cruanes 2016-04-24 22:59:11 +02:00
parent 2c82c3b135
commit 86701af5cc
2 changed files with 26 additions and 0 deletions

View file

@ -580,6 +580,26 @@ let int_range ~start ~stop k =
let int_range_dec ~start ~stop k = let int_range_dec ~start ~stop k =
for i = start downto stop do k i done for i = start downto stop do k i done
let int_range_by ~step i j =
let rec aux step i j yield =
if step>0 then (
if i<=j then (yield i; aux step (i+step) j yield)
) else (
if i>=j then (yield i; aux step (i+step) j yield)
)
in
if step=0 then invalid_arg "int_range_by";
aux step i j
(*$Q
Q.(pair small_int small_int) (fun (i,j) -> \
let i = Pervasives.min i j and j = Pervasives.max i j in \
(i--j |> to_list) = (int_range_by ~step:1 i j |> to_list))
Q.(pair small_int small_int) (fun (i,j) -> \
let i = Pervasives.min i j and j = Pervasives.max i j in \
(i--j |> to_rev_list) = (int_range_by ~step:~-1 j i |> to_list))
*)
let bools k = k false; k true let bools k = k false; k true
let of_set (type s) (type v) m set = let of_set (type s) (type v) m set =

View file

@ -421,6 +421,12 @@ val int_range_dec : start:int -> stop:int -> int t
(** Iterator on decreasing integers in [stop...start] by steps -1. (** Iterator on decreasing integers in [stop...start] by steps -1.
See {!(--^)} for an infix version *) See {!(--^)} for an infix version *)
val int_range_by : step:int -> int -> int -> int t
(** [int_range_by ~step i j] is the range starting at [i], excluding [j],
where the difference between successive elements is [step].
use a negative [step] for a decreasing sequence.
@raise Invalid_argument if [step=0] *)
val bools : bool t val bools : bool t
(** Iterates on [true] and [false] (** Iterates on [true] and [false]
@since 0.7 *) @since 0.7 *)