mirror of
https://github.com/c-cube/iter.git
synced 2025-12-05 19:00:31 -05:00
add int_range_by
This commit is contained in:
parent
2c82c3b135
commit
86701af5cc
2 changed files with 26 additions and 0 deletions
20
sequence.ml
20
sequence.ml
|
|
@ -580,6 +580,26 @@ let int_range ~start ~stop k =
|
|||
let int_range_dec ~start ~stop k =
|
||||
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 of_set (type s) (type v) m set =
|
||||
|
|
|
|||
|
|
@ -421,6 +421,12 @@ val int_range_dec : start:int -> stop:int -> int t
|
|||
(** Iterator on decreasing integers in [stop...start] by steps -1.
|
||||
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
|
||||
(** Iterates on [true] and [false]
|
||||
@since 0.7 *)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue