some small additions

This commit is contained in:
Simon Cruanes 2014-06-14 00:23:09 +02:00
parent 65aceba85e
commit 4579213e63
5 changed files with 22 additions and 4 deletions

View file

@ -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

View file

@ -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 *)

View file

@ -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)

View file

@ -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 *)

View file

@ -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) *)