From 8d41623ba50d20b64b7e1e0e8509cf0e0133fe27 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sat, 26 Mar 2016 12:05:31 +0100 Subject: [PATCH] add `{CCArray,CCVector,CCList}.(--^)` for right-open ranges --- src/core/CCArray.ml | 22 ++++++++++++++++++++++ src/core/CCArray.mli | 4 ++++ src/core/CCList.ml | 8 ++++++++ src/core/CCList.mli | 7 +++++++ src/core/CCVector.ml | 16 ++++++++++++++++ src/core/CCVector.mli | 5 +++++ 6 files changed, 62 insertions(+) diff --git a/src/core/CCArray.ml b/src/core/CCArray.ml index de3b8b43..848952a6 100644 --- a/src/core/CCArray.ml +++ b/src/core/CCArray.ml @@ -443,6 +443,28 @@ let (--) i j = else Array.init (i-j+1) (fun k -> i-k) +(*$T + (1 -- 4) |> Array.to_list = [1;2;3;4] + (4 -- 1) |> Array.to_list = [4;3;2;1] + (0 -- 0) |> Array.to_list = [0] +*) + +(*$Q + Q.(pair small_int small_int) (fun (a,b) -> \ + (a -- b) |> Array.to_list = CCList.(a -- b)) +*) + +let (--^) i j = + if i=j then [| |] + else if i>j + then Array.init (i-j) (fun k -> i-k) + else Array.init (j-i) (fun k -> i+k) + +(*$Q + Q.(pair small_int small_int) (fun (a,b) -> \ + (a --^ b) |> Array.to_list = CCList.(a --^ b)) +*) + (** all the elements of a, but the i-th, into a list *) let except_idx a i = foldi diff --git a/src/core/CCArray.mli b/src/core/CCArray.mli index dd87dd40..71853a1e 100644 --- a/src/core/CCArray.mli +++ b/src/core/CCArray.mli @@ -166,6 +166,10 @@ val except_idx : 'a t -> int -> 'a list val (--) : int -> int -> int t (** Range array *) +val (--^) : int -> int -> int t +(** Range array, excluding right bound + @since NEXT_RELEASE *) + val random : 'a random_gen -> 'a t random_gen val random_non_empty : 'a random_gen -> 'a t random_gen val random_len : int -> 'a random_gen -> 'a t random_gen diff --git a/src/core/CCList.ml b/src/core/CCList.ml index 5d926df0..09a7d067 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -763,11 +763,18 @@ let range' i j = let (--) = range +let (--^) = range' + (*$T append (range 0 100) (range 101 1000) = range 0 1000 append (range 1000 501) (range 500 0) = range 1000 0 *) +(*$Q + Q.(pair small_int small_int) (fun (a,b) -> \ + let l = (a--^b) in not (List.mem b l)) +*) + let replicate i x = let rec aux acc i = if i = 0 then acc @@ -1103,6 +1110,7 @@ module Infix = struct let (<$>) = (<$>) let (>>=) = (>>=) let (--) = (--) + let (--^) = (--^) end (** {2 IO} *) diff --git a/src/core/CCList.mli b/src/core/CCList.mli index 8a9afb25..5da90920 100644 --- a/src/core/CCList.mli +++ b/src/core/CCList.mli @@ -263,6 +263,10 @@ val range' : int -> int -> int t val (--) : int -> int -> int t (** Infix alias for [range] *) +val (--^) : int -> int -> int t +(** Infix alias for [range'] + @since NEXT_RELEASE *) + val replicate : int -> 'a -> 'a t (** Replicate the given element [n] times *) @@ -482,6 +486,9 @@ module Infix : sig val (<$>) : ('a -> 'b) -> 'a t -> 'b t val (>>=) : 'a t -> ('a -> 'b t) -> 'b t val (--) : int -> int -> int t + + val (--^) : int -> int -> int t + (** @since NEXT_RELEASE *) end (** {2 IO} *) diff --git a/src/core/CCVector.ml b/src/core/CCVector.ml index 6eb571e0..0fce2699 100644 --- a/src/core/CCVector.ml +++ b/src/core/CCVector.ml @@ -631,12 +631,28 @@ let (--) i j = then init (i-j+1) (fun k -> i-k) else init (j-i+1) (fun k -> i+k) +(*$Q + Q.(pair small_int small_int) (fun (a,b) -> \ + (a -- b) |> to_list = CCList.(a -- b)) +*) + +let (--^) i j = + if i=j then create() + else if i>j + then init (i-j) (fun k -> i-k) + else init (j-i) (fun k -> i+k) + (*$T (1 -- 4) |> to_list = [1;2;3;4] (4 -- 1) |> to_list = [4;3;2;1] (0 -- 0) |> to_list = [0] *) +(*$Q + Q.(pair small_int small_int) (fun (a,b) -> \ + (a --^ b) |> to_list = CCList.(a --^ b)) +*) + let of_array a = if Array.length a = 0 then create () diff --git a/src/core/CCVector.mli b/src/core/CCVector.mli index ea9088d9..10b5c17d 100644 --- a/src/core/CCVector.mli +++ b/src/core/CCVector.mli @@ -237,6 +237,11 @@ val (--) : int -> int -> (int, 'mut) t therefore the result is never empty). Example: [1 -- 10] returns the vector [[1;2;3;4;5;6;7;8;9;10]] *) +val (--^) : int -> int -> (int, 'mut) t +(** Range of integers, either ascending or descending, but excluding right., + Example: [1 --^ 10] returns the vector [[1;2;3;4;5;6;7;8;9]] + @since NEXT_RELEASE *) + val of_array : 'a array -> ('a, 'mut) t val of_list : 'a list -> ('a, 'mut) t val to_array : ('a,_) t -> 'a array