add CCList.range_by

This commit is contained in:
Glen Mével 2016-04-25 00:56:05 +02:00
parent c800a23a27
commit fb97af680b
2 changed files with 27 additions and 0 deletions

View file

@ -792,6 +792,26 @@ module Idx = struct
*)
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 rec up i j acc =
if i=j then i::acc else up i (j-1) (j::acc)

View file

@ -275,6 +275,13 @@ end
(** {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
(** [range i j] iterates on integers from [i] to [j] included . It works
both for decreasing and increasing ranges *)