merge from master

This commit is contained in:
Simon Cruanes 2014-06-26 22:01:57 +02:00
commit c81a7302fb
9 changed files with 102 additions and 36 deletions

View file

@ -1,9 +1,6 @@
ocaml-containers ocaml-containers
================ ================
A bunch of modules I wrote mostly for fun. It is currently divided into
a few parts:
1. A usable, reasonably well-designed library that extends OCaml's standard 1. A usable, reasonably well-designed library that extends OCaml's standard
library (in `core/`, packaged under `containers` in ocamlfind. Modules library (in `core/`, packaged under `containers` in ocamlfind. Modules
are totally independent and are prefixed with `CC` (for "containers-core" are totally independent and are prefixed with `CC` (for "containers-core"

15
_oasis
View file

@ -1,6 +1,6 @@
OASISFormat: 0.4 OASISFormat: 0.4
Name: containers Name: containers
Version: 0.3 Version: 0.3.1-alpha
Homepage: https://github.com/c-cube/ocaml-containers Homepage: https://github.com/c-cube/ocaml-containers
Authors: Simon Cruanes Authors: Simon Cruanes
License: BSD-2-clause License: BSD-2-clause
@ -9,11 +9,16 @@ Plugins: META (0.3), DevFiles (0.3)
OCamlVersion: >= 4.00.1 OCamlVersion: >= 4.00.1
BuildTools: ocamlbuild BuildTools: ocamlbuild
Synopsis: A bunch of modules, including polymorphic containers. Synopsis: A modular standard library focused on data structures.
Description: Description:
A bunch of useful modules, including polymorphic containers, graph Containers is a standard library (BSD license) focused on data structures,
abstractions, serialization systems, testing systems and various combinators and iterators, without dependencies on unix. Every module is
experiments. independent and is prefixed with 'CC' in the global namespace. Some modules
extend the stdlib (e.g. CCList provides safe map/fold_right/append, and
additional functions on lists).
It also features an optional library for dealing with strings, and a `misc`
library full of experimental ideas (not stable, not necessarily usable).
Flag "misc" Flag "misc"
Description: Build the misc library, containing everything from Description: Build the misc library, containing everything from

View file

@ -159,13 +159,18 @@ let rec _exists2 p a1 a2 i1 i2 j1 =
(* shuffle a[i...j[ using the given int random generator (* shuffle a[i...j[ using the given int random generator
See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle *) See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle *)
let _shuffle _rand_int a i j = let _shuffle _rand_int a i j =
for k = i to j do for k = j-1 downto i+1 do
let l = _rand_int k in let l = _rand_int (k+1) in
let tmp = a.(l) in let tmp = a.(l) in
a.(l) <- a.(k); a.(l) <- a.(k);
a.(l) <- tmp; a.(k) <- tmp;
done done
(*$T
let st = Random.State.make [||] in let a = 0--10000 in \
let b = Array.copy a in shuffle_with st a; a <> b
*)
let _choose a i j st = let _choose a i j st =
if i>=j then raise Not_found; if i>=j then raise Not_found;
a.(i+Random.int (j-i)) a.(i+Random.int (j-i))

View file

@ -58,3 +58,10 @@ let finally ~h ~f =
with e -> with e ->
h (); h ();
raise e raise e
module Monad(X : sig type t end) = struct
type 'a t = X.t -> 'a
let return x _ = x
let (>|=) f g x = g (f x)
let (>>=) f g x = g (f x) x
end

View file

@ -67,3 +67,14 @@ val finally : h:(unit -> unit) -> f:(unit -> 'a) -> 'a
(** [finally h f] calls [f ()] and returns its result. If it raises, the (** [finally h f] calls [f ()] and returns its result. If it raises, the
same exception is raised; in {b any} case, [h ()] is called after same exception is raised; in {b any} case, [h ()] is called after
[f ()] terminates. *) [f ()] terminates. *)
(** {2 Monad}
functions with a fixed domain are monads in their codomain *)
module Monad(X : sig type t end) : sig
type 'a t = X.t -> 'a
val return : 'a -> 'a t
val (>|=) : 'a t -> ('a -> 'b) -> 'b t
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
end

View file

@ -71,6 +71,12 @@ let (<*>) f x = match f, x with
let (<$>) = map let (<$>) = map
let (<+>) a b = match a with
| None -> b
| Some _ -> a
let choice l = List.fold_left (<+>) None l
let map2 f o1 o2 = match o1, o2 with let map2 f o1 o2 = match o1, o2 with
| None, _ | None, _
| _, None -> None | _, None -> None
@ -84,6 +90,24 @@ let fold f acc o = match o with
| None -> acc | None -> acc
| Some x -> f acc x | Some x -> f acc x
let get_exn = function
| Some x -> x
| None -> invalid_arg "CCOpt.get_exn"
let sequence_l l =
let rec aux acc l = match l with
| [] -> Some (List.rev acc)
| Some x :: l' -> aux (x::acc) l'
| None :: _ -> raise Exit
in
try aux [] l with Exit -> None
(*$T
sequence_l [None; Some 1; Some 2] = None
sequence_l [Some 1; Some 2; Some 3] = Some [1;2;3]
sequence_l [] = Some []
*)
let to_list o = match o with let to_list o = match o with
| None -> [] | None -> []
| Some x -> [x] | Some x -> [x]

View file

@ -26,7 +26,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
(** {1 Options} *) (** {1 Options} *)
type 'a t = 'a option type +'a t = 'a option
val map : ('a -> 'b) -> 'a t -> 'b t val map : ('a -> 'b) -> 'a t -> 'b t
(** Transform the element inside, if any *) (** Transform the element inside, if any *)
@ -52,10 +52,6 @@ val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
val flat_map : ('a -> 'b t) -> 'a t -> 'b t val flat_map : ('a -> 'b t) -> 'a t -> 'b t
(** Flip version of {!>>=} *) (** Flip version of {!>>=} *)
val (<*>) : ('a -> 'b) t -> 'a t -> 'b t
val (<$>) : ('a -> 'b) -> 'a t -> 'b t
val map2 : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t val map2 : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t
val iter : ('a -> unit) -> 'a t -> unit val iter : ('a -> unit) -> 'a t -> unit
@ -64,6 +60,26 @@ val iter : ('a -> unit) -> 'a t -> unit
val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a
(** Fold on 0 or 1 elements *) (** Fold on 0 or 1 elements *)
val get_exn : 'a t -> 'a
(** Open the option, possibly failing if it is [None]
@raise Invalid_argument if the option is [None] *)
val sequence_l : 'a t list -> 'a list t
(** {2 Applicative} *)
val (<*>) : ('a -> 'b) t -> 'a t -> 'b t
val (<$>) : ('a -> 'b) -> 'a t -> 'b t
(** {2 Alternatives} *)
val (<+>) : 'a t -> 'a t -> 'a t
(** [a <+> b] is [a] if [a] is [Some _], [b] otherwise *)
val choice : 'a t list -> 'a t
(** [choice] returns the first non-[None] element of the list, or [None] *)
(** {2 Conversion and IO} *) (** {2 Conversion and IO} *)
val to_list : 'a t -> 'a list val to_list : 'a t -> 'a list

View file

@ -1,15 +1,15 @@
# OASIS_START # OASIS_START
# DO NOT EDIT (digest: 854ed9ebed0116b5b8aa2f30b4b72738) # DO NOT EDIT (digest: 38ca5655da37b1039f2aa919ef60de51)
version = "0.3" version = "0.3.1-alpha"
description = "A bunch of modules, including polymorphic containers." description = "A modular standard library focused on data structures."
archive(byte) = "containers.cma" archive(byte) = "containers.cma"
archive(byte, plugin) = "containers.cma" archive(byte, plugin) = "containers.cma"
archive(native) = "containers.cmxa" archive(native) = "containers.cmxa"
archive(native, plugin) = "containers.cmxs" archive(native, plugin) = "containers.cmxs"
exists_if = "containers.cma" exists_if = "containers.cma"
package "thread" ( package "thread" (
version = "0.3" version = "0.3.1-alpha"
description = "A bunch of modules, including polymorphic containers." description = "A modular standard library focused on data structures."
requires = "containers threads lwt" requires = "containers threads lwt"
archive(byte) = "containers_thread.cma" archive(byte) = "containers_thread.cma"
archive(byte, plugin) = "containers_thread.cma" archive(byte, plugin) = "containers_thread.cma"
@ -19,8 +19,8 @@ package "thread" (
) )
package "string" ( package "string" (
version = "0.3" version = "0.3.1-alpha"
description = "A bunch of modules, including polymorphic containers." description = "A modular standard library focused on data structures."
archive(byte) = "containers_string.cma" archive(byte) = "containers_string.cma"
archive(byte, plugin) = "containers_string.cma" archive(byte, plugin) = "containers_string.cma"
archive(native) = "containers_string.cmxa" archive(native) = "containers_string.cmxa"
@ -29,8 +29,8 @@ package "string" (
) )
package "misc" ( package "misc" (
version = "0.3" version = "0.3.1-alpha"
description = "A bunch of modules, including polymorphic containers." description = "A modular standard library focused on data structures."
requires = "unix containers" requires = "unix containers"
archive(byte) = "containers_misc.cma" archive(byte) = "containers_misc.cma"
archive(byte, plugin) = "containers_misc.cma" archive(byte, plugin) = "containers_misc.cma"
@ -40,8 +40,8 @@ package "misc" (
) )
package "lwt" ( package "lwt" (
version = "0.3" version = "0.3.1-alpha"
description = "A bunch of modules, including polymorphic containers." description = "A modular standard library focused on data structures."
requires = "containers lwt lwt.unix containers.misc" requires = "containers lwt lwt.unix containers.misc"
archive(byte) = "containers_lwt.cma" archive(byte) = "containers_lwt.cma"
archive(byte, plugin) = "containers_lwt.cma" archive(byte, plugin) = "containers_lwt.cma"
@ -51,8 +51,8 @@ package "lwt" (
) )
package "cgi" ( package "cgi" (
version = "0.3" version = "0.3.1-alpha"
description = "A bunch of modules, including polymorphic containers." description = "A modular standard library focused on data structures."
requires = "containers CamlGI" requires = "containers CamlGI"
archive(byte) = "containers_cgi.cma" archive(byte) = "containers_cgi.cma"
archive(byte, plugin) = "containers_cgi.cma" archive(byte, plugin) = "containers_cgi.cma"

View file

@ -1,7 +1,7 @@
(* setup.ml generated for the first time by OASIS v0.4.4 *) (* setup.ml generated for the first time by OASIS v0.4.4 *)
(* OASIS_START *) (* OASIS_START *)
(* DO NOT EDIT (digest: 4ada3be2c7f1b78d0a6742ac05a3203d) *) (* DO NOT EDIT (digest: af57fb786b958cce0ff1ad15f9c9c1c6) *)
(* (*
Regenerated by OASIS v0.4.4 Regenerated by OASIS v0.4.4
Visit http://oasis.forge.ocamlcore.org for more information and Visit http://oasis.forge.ocamlcore.org for more information and
@ -6856,7 +6856,7 @@ let setup_t =
alpha_features = []; alpha_features = [];
beta_features = []; beta_features = [];
name = "containers"; name = "containers";
version = "0.3"; version = "0.3.1-alpha";
license = license =
OASISLicense.DEP5License OASISLicense.DEP5License
(OASISLicense.DEP5Unit (OASISLicense.DEP5Unit
@ -6870,12 +6870,12 @@ let setup_t =
maintainers = []; maintainers = [];
authors = ["Simon Cruanes"]; authors = ["Simon Cruanes"];
homepage = Some "https://github.com/c-cube/ocaml-containers"; homepage = Some "https://github.com/c-cube/ocaml-containers";
synopsis = "A bunch of modules, including polymorphic containers."; synopsis = "A modular standard library focused on data structures.";
description = description =
Some Some
[ [
OASISText.Para OASISText.Para
"A bunch of useful modules, including polymorphic containers, graph abstractions, serialization systems, testing systems and various experiments." "Containers is a standard library (BSD license) focused on data structures, combinators and iterators, without dependencies on unix. Every module is independent and is prefixed with 'CC' in the global namespace. Some modules extend the stdlib (e.g. CCList provides safe map/fold_right/append, and additional functions on lists). It also features an optional library for dealing with strings, and a `misc` library full of experimental ideas (not stable, not necessarily usable)."
]; ];
categories = []; categories = [];
conf_type = (`Configure, "internal", Some "0.4"); conf_type = (`Configure, "internal", Some "0.4");
@ -7736,7 +7736,8 @@ let setup_t =
}; };
oasis_fn = Some "_oasis"; oasis_fn = Some "_oasis";
oasis_version = "0.4.4"; oasis_version = "0.4.4";
oasis_digest = Some "\167\b\207\220E\000\132i\189/}I|<\145\185"; oasis_digest =
Some "\222\021\175\246\227n\021L\206\022\182\224\189J\203\168";
oasis_exec = None; oasis_exec = None;
oasis_setup_args = []; oasis_setup_args = [];
setup_update = false setup_update = false
@ -7744,6 +7745,6 @@ let setup_t =
let setup () = BaseSetup.setup setup_t;; let setup () = BaseSetup.setup setup_t;;
# 7748 "setup.ml" # 7749 "setup.ml"
(* OASIS_STOP *) (* OASIS_STOP *)
let () = setup ();; let () = setup ();;