mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-08 12:15:32 -05:00
some small additions
This commit is contained in:
parent
65aceba85e
commit
4579213e63
5 changed files with 22 additions and 4 deletions
|
|
@ -141,14 +141,18 @@ let except_idx a i =
|
||||||
|
|
||||||
(* Randomly shuffle the array, in place.
|
(* Randomly shuffle the array, in place.
|
||||||
See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle *)
|
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
|
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
|
let tmp = a.(i) in
|
||||||
a.(i) <- a.(j);
|
a.(i) <- a.(j);
|
||||||
a.(j) <- tmp;
|
a.(j) <- tmp;
|
||||||
done
|
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 *)
|
(** print an array of items using the printing function *)
|
||||||
let pp ?(sep=", ") pp_item buf a =
|
let pp ?(sep=", ") pp_item buf a =
|
||||||
for i = 0 to Array.length a - 1 do
|
for i = 0 to Array.length a - 1 do
|
||||||
|
|
|
||||||
|
|
@ -67,6 +67,9 @@ val except_idx : 'a t -> int -> 'a list
|
||||||
val shuffle : 'a t -> unit
|
val shuffle : 'a t -> unit
|
||||||
(** shuffle randomly the array, in place *)
|
(** 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)
|
val pp: ?sep:string -> (Buffer.t -> 'a -> unit)
|
||||||
-> Buffer.t -> 'a t -> unit
|
-> Buffer.t -> 'a t -> unit
|
||||||
(** print an array of items with printing function *)
|
(** print an array of items with printing function *)
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,8 @@ let const x _ = x
|
||||||
|
|
||||||
let uncurry f (x,y) = f x y
|
let uncurry f (x,y) = f x y
|
||||||
|
|
||||||
|
let tap f x = ignore (f x); x
|
||||||
|
|
||||||
let (%>) = compose
|
let (%>) = compose
|
||||||
|
|
||||||
let (%) f g x = f (g x)
|
let (%) f g x = f (g x)
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,15 @@ val curry : ('a * 'b -> 'c) -> 'a -> 'b -> 'c
|
||||||
|
|
||||||
val uncurry : ('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
|
val (%) : ('b -> 'c) -> ('a -> 'b) -> 'a -> 'c
|
||||||
(** Mathematical composition *)
|
(** Mathematical composition *)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,8 +25,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
(** {1 Growable, mutable vector} *)
|
(** {1 Growable, mutable vector} *)
|
||||||
|
|
||||||
type ro
|
type ro = [`RO]
|
||||||
type rw
|
type rw = [`RW]
|
||||||
|
|
||||||
(** Mutability is [rw] (read-write) or [ro] (read-only) *)
|
(** Mutability is [rw] (read-write) or [ro] (read-only) *)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue