From 4579213e6323757ce2471d729841295cc5a8f47d Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sat, 14 Jun 2014 00:23:09 +0200 Subject: [PATCH] some small additions --- core/CCArray.ml | 8 ++++++-- core/CCArray.mli | 3 +++ core/CCFun.ml | 2 ++ core/CCFun.mli | 9 +++++++++ core/CCVector.mli | 4 ++-- 5 files changed, 22 insertions(+), 4 deletions(-) diff --git a/core/CCArray.ml b/core/CCArray.ml index 4f7ad5c6..2838c7cc 100644 --- a/core/CCArray.ml +++ b/core/CCArray.ml @@ -141,14 +141,18 @@ let except_idx a i = (* Randomly shuffle the array, in place. See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle *) -let shuffle a = +let _shuffle _rand_int a = for i = 1 to Array.length a - 1 do - let j = Random.int i in + let j = _rand_int i in let tmp = a.(i) in a.(i) <- a.(j); a.(j) <- tmp; done +let shuffle a = _shuffle Random.int a + +let shuffle_with st a = _shuffle (Random.State.int st) a + (** print an array of items using the printing function *) let pp ?(sep=", ") pp_item buf a = for i = 0 to Array.length a - 1 do diff --git a/core/CCArray.mli b/core/CCArray.mli index 555a77c8..e31ce665 100644 --- a/core/CCArray.mli +++ b/core/CCArray.mli @@ -67,6 +67,9 @@ val except_idx : 'a t -> int -> 'a list val shuffle : 'a t -> unit (** shuffle randomly the array, in place *) +val shuffle_with : Random.State.t -> 'a t -> unit +(** Like shuffle but using a specialized random state *) + val pp: ?sep:string -> (Buffer.t -> 'a -> unit) -> Buffer.t -> 'a t -> unit (** print an array of items with printing function *) diff --git a/core/CCFun.ml b/core/CCFun.ml index dbedcf9b..fa4eadb6 100644 --- a/core/CCFun.ml +++ b/core/CCFun.ml @@ -40,6 +40,8 @@ let const x _ = x let uncurry f (x,y) = f x y +let tap f x = ignore (f x); x + let (%>) = compose let (%) f g x = f (g x) diff --git a/core/CCFun.mli b/core/CCFun.mli index d2a02f3e..81048ae0 100644 --- a/core/CCFun.mli +++ b/core/CCFun.mli @@ -48,6 +48,15 @@ val curry : ('a * 'b -> 'c) -> 'a -> 'b -> 'c val uncurry : ('a -> 'b -> 'c) -> ('a * 'b) -> 'c +val tap : ('a -> 'b) -> 'a -> 'a +(** [tap f x] evaluates [f x], discards it, then returns [x]. Useful + in a pipeline, for instance: + {[CCArray.(1 -- 10) + |> tap CCArray.shuffle + |> tap CCArray.sort Pervasives.compare + ]} +*) + val (%) : ('b -> 'c) -> ('a -> 'b) -> 'a -> 'c (** Mathematical composition *) diff --git a/core/CCVector.mli b/core/CCVector.mli index 4d8cd997..e981f6f5 100644 --- a/core/CCVector.mli +++ b/core/CCVector.mli @@ -25,8 +25,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. (** {1 Growable, mutable vector} *) -type ro -type rw +type ro = [`RO] +type rw = [`RW] (** Mutability is [rw] (read-write) or [ro] (read-only) *)