From fb97af680b92d83f675a28ef5a263c828b61525b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Glen=20M=C3=A9vel?= Date: Mon, 25 Apr 2016 00:56:05 +0200 Subject: [PATCH 1/2] add CCList.range_by --- src/core/CCList.ml | 20 ++++++++++++++++++++ src/core/CCList.mli | 7 +++++++ 2 files changed, 27 insertions(+) diff --git a/src/core/CCList.ml b/src/core/CCList.ml index 857512ce..975a0861 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -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 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) diff --git a/src/core/CCList.mli b/src/core/CCList.mli index 41f4e5d0..8bb1ba7e 100644 --- a/src/core/CCList.mli +++ b/src/core/CCList.mli @@ -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 *) From f57d7267ac54ac6f942a74c6da78d034b9f013ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Glen=20M=C3=A9vel?= Date: Mon, 25 Apr 2016 11:39:03 +0200 Subject: [PATCH 2/2] bug fix for CCList.range_by --- src/core/CCList.ml | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/core/CCList.ml b/src/core/CCList.ml index 975a0861..80b8578f 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -793,23 +793,29 @@ 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 0 then up i j [] else down i j [] + if step = 0 then + raise (Invalid_argument "CCList.range_by") + else if (if step > 0 then i>j else i