mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-07 03:35:30 -05:00
add CCList.range_by
This commit is contained in:
parent
c800a23a27
commit
fb97af680b
2 changed files with 27 additions and 0 deletions
|
|
@ -792,6 +792,26 @@ module Idx = struct
|
||||||
*)
|
*)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
let range_by ~step i j =
|
||||||
|
if step = 0 then raise (Invalid_argument "CCList.range_by");
|
||||||
|
let rec up i j acc =
|
||||||
|
if i>j then acc else up i (j-step) (j::acc)
|
||||||
|
and down i j acc =
|
||||||
|
if i<j then acc else down i (j-step) (j::acc)
|
||||||
|
in
|
||||||
|
let j = (j - i) / step * step + i in
|
||||||
|
if step > 0 then up i j [] else down i j []
|
||||||
|
|
||||||
|
(*$T
|
||||||
|
range_by ~step:1 0 0 = [0]
|
||||||
|
range_by ~step:1 5 0 = []
|
||||||
|
range_by ~step:2 0 4 = [0;2;4]
|
||||||
|
range_by ~step:2 0 5 = [0;2;4]
|
||||||
|
range_by ~step:~-1 0 5 = []
|
||||||
|
range_by ~step:~-2 5 1 = [5;3;1]
|
||||||
|
range_by ~step:~-2 5 0 = [5;3;1]
|
||||||
|
*)
|
||||||
|
|
||||||
let range i j =
|
let range i j =
|
||||||
let rec up i j acc =
|
let rec up i j acc =
|
||||||
if i=j then i::acc else up i (j-1) (j::acc)
|
if i=j then i::acc else up i (j-1) (j::acc)
|
||||||
|
|
|
||||||
|
|
@ -275,6 +275,13 @@ end
|
||||||
|
|
||||||
(** {2 Other Constructors} *)
|
(** {2 Other Constructors} *)
|
||||||
|
|
||||||
|
val range_by : step:int -> int -> int -> int t
|
||||||
|
(** [range_by ~step i j] iterates on integers from [i] to [j] included,
|
||||||
|
where the difference between successive elements is [step].
|
||||||
|
use a negative [step] for a decreasing list.
|
||||||
|
@raise Invalid_argument if [step=0]
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
val range : int -> int -> int t
|
val range : int -> int -> int t
|
||||||
(** [range i j] iterates on integers from [i] to [j] included . It works
|
(** [range i j] iterates on integers from [i] to [j] included . It works
|
||||||
both for decreasing and increasing ranges *)
|
both for decreasing and increasing ranges *)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue