mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2026-01-28 11:54:51 -05:00
add {CCArray,CCVector,CCList}.(--^) for right-open ranges
This commit is contained in:
parent
6ccad958c4
commit
8d41623ba5
6 changed files with 62 additions and 0 deletions
|
|
@ -443,6 +443,28 @@ let (--) i j =
|
||||||
else
|
else
|
||||||
Array.init (i-j+1) (fun k -> i-k)
|
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 *)
|
(** all the elements of a, but the i-th, into a list *)
|
||||||
let except_idx a i =
|
let except_idx a i =
|
||||||
foldi
|
foldi
|
||||||
|
|
|
||||||
|
|
@ -166,6 +166,10 @@ val except_idx : 'a t -> int -> 'a list
|
||||||
val (--) : int -> int -> int t
|
val (--) : int -> int -> int t
|
||||||
(** Range array *)
|
(** 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 : 'a random_gen -> 'a t random_gen
|
||||||
val random_non_empty : '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
|
val random_len : int -> 'a random_gen -> 'a t random_gen
|
||||||
|
|
|
||||||
|
|
@ -763,11 +763,18 @@ let range' i j =
|
||||||
|
|
||||||
let (--) = range
|
let (--) = range
|
||||||
|
|
||||||
|
let (--^) = range'
|
||||||
|
|
||||||
(*$T
|
(*$T
|
||||||
append (range 0 100) (range 101 1000) = range 0 1000
|
append (range 0 100) (range 101 1000) = range 0 1000
|
||||||
append (range 1000 501) (range 500 0) = range 1000 0
|
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 replicate i x =
|
||||||
let rec aux acc i =
|
let rec aux acc i =
|
||||||
if i = 0 then acc
|
if i = 0 then acc
|
||||||
|
|
@ -1103,6 +1110,7 @@ module Infix = struct
|
||||||
let (<$>) = (<$>)
|
let (<$>) = (<$>)
|
||||||
let (>>=) = (>>=)
|
let (>>=) = (>>=)
|
||||||
let (--) = (--)
|
let (--) = (--)
|
||||||
|
let (--^) = (--^)
|
||||||
end
|
end
|
||||||
|
|
||||||
(** {2 IO} *)
|
(** {2 IO} *)
|
||||||
|
|
|
||||||
|
|
@ -263,6 +263,10 @@ val range' : int -> int -> int t
|
||||||
val (--) : int -> int -> int t
|
val (--) : int -> int -> int t
|
||||||
(** Infix alias for [range] *)
|
(** Infix alias for [range] *)
|
||||||
|
|
||||||
|
val (--^) : int -> int -> int t
|
||||||
|
(** Infix alias for [range']
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
val replicate : int -> 'a -> 'a t
|
val replicate : int -> 'a -> 'a t
|
||||||
(** Replicate the given element [n] times *)
|
(** Replicate the given element [n] times *)
|
||||||
|
|
||||||
|
|
@ -482,6 +486,9 @@ module Infix : sig
|
||||||
val (<$>) : ('a -> 'b) -> 'a t -> 'b t
|
val (<$>) : ('a -> 'b) -> 'a t -> 'b t
|
||||||
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
|
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
|
||||||
val (--) : int -> int -> int t
|
val (--) : int -> int -> int t
|
||||||
|
|
||||||
|
val (--^) : int -> int -> int t
|
||||||
|
(** @since NEXT_RELEASE *)
|
||||||
end
|
end
|
||||||
|
|
||||||
(** {2 IO} *)
|
(** {2 IO} *)
|
||||||
|
|
|
||||||
|
|
@ -631,12 +631,28 @@ let (--) i j =
|
||||||
then init (i-j+1) (fun k -> i-k)
|
then init (i-j+1) (fun k -> i-k)
|
||||||
else init (j-i+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
|
(*$T
|
||||||
(1 -- 4) |> to_list = [1;2;3;4]
|
(1 -- 4) |> to_list = [1;2;3;4]
|
||||||
(4 -- 1) |> to_list = [4;3;2;1]
|
(4 -- 1) |> to_list = [4;3;2;1]
|
||||||
(0 -- 0) |> to_list = [0]
|
(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 =
|
let of_array a =
|
||||||
if Array.length a = 0
|
if Array.length a = 0
|
||||||
then create ()
|
then create ()
|
||||||
|
|
|
||||||
|
|
@ -237,6 +237,11 @@ val (--) : int -> int -> (int, 'mut) t
|
||||||
therefore the result is never empty).
|
therefore the result is never empty).
|
||||||
Example: [1 -- 10] returns the vector [[1;2;3;4;5;6;7;8;9;10]] *)
|
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_array : 'a array -> ('a, 'mut) t
|
||||||
val of_list : 'a list -> ('a, 'mut) t
|
val of_list : 'a list -> ('a, 'mut) t
|
||||||
val to_array : ('a,_) t -> 'a array
|
val to_array : ('a,_) t -> 'a array
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue