mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-08 12:15:32 -05:00
more modules in containers Core;
moved KMP to sublibrary containers_string
This commit is contained in:
parent
4bc6c8a008
commit
3ccabcd7b0
14 changed files with 672 additions and 6 deletions
|
|
@ -1,12 +1,13 @@
|
||||||
#use "topfind";;
|
#use "topfind";;
|
||||||
#thread
|
#thread
|
||||||
#directory "_build/";;
|
#directory "_build/core";;
|
||||||
|
#directory "_build/misc";;
|
||||||
#directory "_build/tests/";;
|
#directory "_build/tests/";;
|
||||||
#load "containers.cma";;
|
#load "containers.cma";;
|
||||||
|
#load "containers_misc.cma";;
|
||||||
#require "threads";;
|
#require "threads";;
|
||||||
#load "containers_thread.cma";;
|
#load "containers_thread.cma";;
|
||||||
open Containers;;
|
open Containers;;
|
||||||
open Sequence.Infix;;
|
|
||||||
#install_printer Bencode.pretty;;
|
#install_printer Bencode.pretty;;
|
||||||
#install_printer HGraph.Default.fmt;;
|
#install_printer HGraph.Default.fmt;;
|
||||||
#require "CamlGI";;
|
#require "CamlGI";;
|
||||||
|
|
|
||||||
11
_oasis
11
_oasis
|
|
@ -39,10 +39,17 @@ Flag "bench"
|
||||||
Library "containers"
|
Library "containers"
|
||||||
Path: core
|
Path: core
|
||||||
Modules: CCVector, CCDeque, CCGen, CCSequence, CCFQueue, CCMultiMap,
|
Modules: CCVector, CCDeque, CCGen, CCSequence, CCFQueue, CCMultiMap,
|
||||||
CCMultiSet, CCBV, CCKMP, CCPrint, CCPersistentHashtbl,
|
CCMultiSet, CCBV, CCPrint, CCPersistentHashtbl,
|
||||||
CCLeftistheap
|
CCLeftistheap, CCList, CCOpt, CCPair, CCFun
|
||||||
FindlibName: containers
|
FindlibName: containers
|
||||||
|
|
||||||
|
Library "containers_string"
|
||||||
|
Path: string
|
||||||
|
Pack: true
|
||||||
|
Modules: KMP
|
||||||
|
FindlibName: string
|
||||||
|
FindlibParent: containers
|
||||||
|
|
||||||
Library "containers_misc"
|
Library "containers_misc"
|
||||||
Path: misc
|
Path: misc
|
||||||
Pack: true
|
Pack: true
|
||||||
|
|
|
||||||
39
core/CCFun.ml
Normal file
39
core/CCFun.ml
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
|
||||||
|
(*
|
||||||
|
copyright (c) 2013-2014, simon cruanes
|
||||||
|
all rights reserved.
|
||||||
|
|
||||||
|
redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer. redistributions in binary
|
||||||
|
form must reproduce the above copyright notice, this list of conditions and the
|
||||||
|
following disclaimer in the documentation and/or other materials provided with
|
||||||
|
the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||||
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*)
|
||||||
|
|
||||||
|
(** {1 Basic Functions} *)
|
||||||
|
|
||||||
|
let (|>) x f = f x
|
||||||
|
|
||||||
|
let compose f g x = g (f x)
|
||||||
|
|
||||||
|
let (%>) = compose
|
||||||
|
|
||||||
|
let (%) f g x = f (g x)
|
||||||
|
|
||||||
|
let lexicographic f1 f2 x y =
|
||||||
|
let c = f1 x y in
|
||||||
|
if c <> 0 then c else f2 x y
|
||||||
42
core/CCFun.mli
Normal file
42
core/CCFun.mli
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
|
||||||
|
(*
|
||||||
|
copyright (c) 2013-2014, simon cruanes
|
||||||
|
all rights reserved.
|
||||||
|
|
||||||
|
redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer. redistributions in binary
|
||||||
|
form must reproduce the above copyright notice, this list of conditions and the
|
||||||
|
following disclaimer in the documentation and/or other materials provided with
|
||||||
|
the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||||
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*)
|
||||||
|
|
||||||
|
(** {1 Basic Functions} *)
|
||||||
|
|
||||||
|
val (|>) : 'a -> ('a -> 'b) -> 'b
|
||||||
|
(** Pipeline (naive implementation) *)
|
||||||
|
|
||||||
|
val compose : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c
|
||||||
|
(** Composition *)
|
||||||
|
|
||||||
|
val (%>) : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c
|
||||||
|
(** Alias to [compose] *)
|
||||||
|
|
||||||
|
val (%) : ('b -> 'c) -> ('a -> 'b) -> 'a -> 'c
|
||||||
|
(** Mathematical composition *)
|
||||||
|
|
||||||
|
val lexicographic : ('a -> 'a -> int) -> ('a -> 'a -> int) -> 'a -> 'a -> int
|
||||||
|
(** Lexicographic combination of comparison functions *)
|
||||||
213
core/CCList.ml
Normal file
213
core/CCList.ml
Normal file
|
|
@ -0,0 +1,213 @@
|
||||||
|
|
||||||
|
(*
|
||||||
|
copyright (c) 2013-2014, simon cruanes
|
||||||
|
all rights reserved.
|
||||||
|
|
||||||
|
redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer. redistributions in binary
|
||||||
|
form must reproduce the above copyright notice, this list of conditions and the
|
||||||
|
following disclaimer in the documentation and/or other materials provided with
|
||||||
|
the distribution.
|
||||||
|
|
||||||
|
this software is provided by the copyright holders and contributors "as is" and
|
||||||
|
any express or implied warranties, including, but not limited to, the implied
|
||||||
|
warranties of merchantability and fitness for a particular purpose are
|
||||||
|
disclaimed. in no event shall the copyright holder or contributors be liable
|
||||||
|
for any direct, indirect, incidental, special, exemplary, or consequential
|
||||||
|
damages (including, but not limited to, procurement of substitute goods or
|
||||||
|
services; loss of use, data, or profits; or business interruption) however
|
||||||
|
caused and on any theory of liability, whether in contract, strict liability,
|
||||||
|
or tort (including negligence or otherwise) arising in any way out of the use
|
||||||
|
of this software, even if advised of the possibility of such damage.
|
||||||
|
*)
|
||||||
|
|
||||||
|
(** {1 complements to list} *)
|
||||||
|
|
||||||
|
type 'a t = 'a list
|
||||||
|
|
||||||
|
(* max depth for direct recursion *)
|
||||||
|
let _direct_depth = 500
|
||||||
|
|
||||||
|
let map f l =
|
||||||
|
let rec direct f i l = match l with
|
||||||
|
| [] -> []
|
||||||
|
| _ when i=0 -> safe f l
|
||||||
|
| x::l' ->
|
||||||
|
let y = f x in
|
||||||
|
y :: direct f (i-1) l'
|
||||||
|
and safe f l =
|
||||||
|
List.rev (List.rev_map f l)
|
||||||
|
in
|
||||||
|
direct f _direct_depth l
|
||||||
|
|
||||||
|
(*$Q
|
||||||
|
(Q.list Q.small_int) (fun l -> \
|
||||||
|
let f x = x+1 in \
|
||||||
|
List.rev (List.rev_map f l) = map f l)
|
||||||
|
*)
|
||||||
|
|
||||||
|
let append l1 l2 =
|
||||||
|
let rec direct i l1 l2 = match l1 with
|
||||||
|
| [] -> l2
|
||||||
|
| _ when i=0 -> safe l1 l2
|
||||||
|
| x::l1' -> x :: direct (i-1) l1' l2
|
||||||
|
and safe l1 l2 =
|
||||||
|
List.rev_append (List.rev l1) l2
|
||||||
|
in
|
||||||
|
direct _direct_depth l1 l2
|
||||||
|
|
||||||
|
let (@) = append
|
||||||
|
|
||||||
|
let rec compare f l1 l2 = match l1, l2 with
|
||||||
|
| [], [] -> 0
|
||||||
|
| _, [] -> 1
|
||||||
|
| [], _ -> -1
|
||||||
|
| x1::l1', x2::l2' ->
|
||||||
|
let c = f x1 x2 in
|
||||||
|
if c <> 0 then c else compare f l1' l2'
|
||||||
|
|
||||||
|
let rec equal f l1 l2 = match l1, l2 with
|
||||||
|
| [], [] -> true
|
||||||
|
| [], _ | _, [] -> false
|
||||||
|
| x1::l1', x2::l2' -> f x1 x2 && equal f l1' l2'
|
||||||
|
|
||||||
|
(* difference list *)
|
||||||
|
type 'a dlist = 'a list -> 'a list
|
||||||
|
|
||||||
|
(* append difference lists *)
|
||||||
|
let _d_append f1 f2 =
|
||||||
|
fun l -> f1 (f2 l)
|
||||||
|
|
||||||
|
let flat_map f l =
|
||||||
|
let rec aux prefix f l = match l with
|
||||||
|
| [] -> prefix []
|
||||||
|
| x::l' ->
|
||||||
|
let sublist = append (f x) in
|
||||||
|
let prefix = _d_append prefix sublist in
|
||||||
|
aux prefix f l'
|
||||||
|
in
|
||||||
|
aux (fun l->l) f l
|
||||||
|
|
||||||
|
(*$T
|
||||||
|
flat_map (fun x -> [x+1; x*2]) [10;100] = [11;20;101;200]
|
||||||
|
*)
|
||||||
|
|
||||||
|
let flatten l = flat_map (fun l -> l) l
|
||||||
|
|
||||||
|
let product f l1 l2 =
|
||||||
|
flat_map (fun x -> map (fun y -> f x y) l2) l1
|
||||||
|
|
||||||
|
let return x = [x]
|
||||||
|
|
||||||
|
let (>>=) l f = flat_map f l
|
||||||
|
|
||||||
|
let (<$>) = map
|
||||||
|
|
||||||
|
let (<*>) funs l = product (fun f x -> f x) funs l
|
||||||
|
|
||||||
|
(*$T
|
||||||
|
List.sort Pervasives.compare ([(( * )2); ((+)1)] <*> [10;100]) \
|
||||||
|
= [11; 20; 101; 200]
|
||||||
|
*)
|
||||||
|
|
||||||
|
let range i j =
|
||||||
|
let rec up i j acc =
|
||||||
|
if i=j then i::acc else up i (j-1) (j::acc)
|
||||||
|
and down i j acc =
|
||||||
|
if i=j then i::acc else down i (j+1) (j::acc)
|
||||||
|
in
|
||||||
|
if i<=j then up i j [] else down i j []
|
||||||
|
|
||||||
|
(*$T
|
||||||
|
range 0 5 = [0;1;2;3;4;5]
|
||||||
|
range 0 0 = [0]
|
||||||
|
range 5 2 = [5;4;3;2]
|
||||||
|
*)
|
||||||
|
|
||||||
|
let (--) = range
|
||||||
|
|
||||||
|
(*$T
|
||||||
|
append (range 0 100) (range 101 1000) = range 0 1000
|
||||||
|
append (range 1000 500) (range 499 0) = range 1000 0
|
||||||
|
*)
|
||||||
|
|
||||||
|
let take n l =
|
||||||
|
let rec direct i n l = match l with
|
||||||
|
| [] -> []
|
||||||
|
| _ when i=0 -> safe n [] l
|
||||||
|
| x::l' -> x :: direct (i-1) (n-1) l'
|
||||||
|
and safe n acc l = match l with
|
||||||
|
| [] -> List.rev acc
|
||||||
|
| _ when n=0 -> List.rev acc
|
||||||
|
| x::l' -> safe (n-1) (x::acc) l'
|
||||||
|
in
|
||||||
|
direct _direct_depth n l
|
||||||
|
|
||||||
|
let rec drop n l = match l with
|
||||||
|
| [] -> []
|
||||||
|
| _ when n=0 -> l
|
||||||
|
| _::l' -> drop (n-1) l'
|
||||||
|
|
||||||
|
let split n l = take n l, drop n l
|
||||||
|
|
||||||
|
(*$Q
|
||||||
|
(Q.pair (Q.list Q.small_int) Q.int) (fun (l,i) -> \
|
||||||
|
let i = abs i in \
|
||||||
|
let l1, l2 = split i l in \
|
||||||
|
l1 @ l2 = l )
|
||||||
|
*)
|
||||||
|
|
||||||
|
let last n l =
|
||||||
|
let len = List.length l in
|
||||||
|
if len < n then l else drop (len-n) l
|
||||||
|
|
||||||
|
(** {2 Conversions} *)
|
||||||
|
|
||||||
|
type 'a sequence = ('a -> unit) -> unit
|
||||||
|
type 'a gen = unit -> 'a option
|
||||||
|
type 'a printer = Buffer.t -> 'a -> unit
|
||||||
|
|
||||||
|
let to_seq l k = List.iter k l
|
||||||
|
let of_seq seq =
|
||||||
|
let l = ref [] in
|
||||||
|
seq (fun x -> l := x :: !l);
|
||||||
|
List.rev !l
|
||||||
|
|
||||||
|
let to_gen l =
|
||||||
|
let l = ref l in
|
||||||
|
fun () ->
|
||||||
|
match !l with
|
||||||
|
| [] -> None
|
||||||
|
| x::l' ->
|
||||||
|
l := l'; Some x
|
||||||
|
|
||||||
|
let of_gen g =
|
||||||
|
let rec direct i g =
|
||||||
|
if i = 0 then safe [] g
|
||||||
|
else match g () with
|
||||||
|
| None -> []
|
||||||
|
| Some x -> x :: direct (i-1) g
|
||||||
|
and safe acc g = match g () with
|
||||||
|
| None -> List.rev acc
|
||||||
|
| Some x -> safe (x::acc) g
|
||||||
|
in
|
||||||
|
direct _direct_depth g
|
||||||
|
|
||||||
|
(** {2 IO} *)
|
||||||
|
|
||||||
|
let pp ?(start="[") ?(stop="]") ?(sep=", ") pp_item buf l =
|
||||||
|
let rec print l = match l with
|
||||||
|
| x::((y::xs) as l) ->
|
||||||
|
pp_item buf x;
|
||||||
|
Buffer.add_string buf sep;
|
||||||
|
print l
|
||||||
|
| x::[] -> pp_item buf x
|
||||||
|
| [] -> ()
|
||||||
|
in Buffer.add_string buf start; print l; Buffer.add_string buf stop
|
||||||
|
|
||||||
|
(*$T
|
||||||
|
CCPrint.to_string (pp CCPrint.int) [1;2;3] = "[1, 2, 3]"
|
||||||
|
*)
|
||||||
96
core/CCList.mli
Normal file
96
core/CCList.mli
Normal file
|
|
@ -0,0 +1,96 @@
|
||||||
|
|
||||||
|
(*
|
||||||
|
copyright (c) 2013-2014, simon cruanes
|
||||||
|
all rights reserved.
|
||||||
|
|
||||||
|
redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer. redistributions in binary
|
||||||
|
form must reproduce the above copyright notice, this list of conditions and the
|
||||||
|
following disclaimer in the documentation and/or other materials provided with
|
||||||
|
the distribution.
|
||||||
|
|
||||||
|
this software is provided by the copyright holders and contributors "as is" and
|
||||||
|
any express or implied warranties, including, but not limited to, the implied
|
||||||
|
warranties of merchantability and fitness for a particular purpose are
|
||||||
|
disclaimed. in no event shall the copyright holder or contributors be liable
|
||||||
|
for any direct, indirect, incidental, special, exemplary, or consequential
|
||||||
|
damages (including, but not limited to, procurement of substitute goods or
|
||||||
|
services; loss of use, data, or profits; or business interruption) however
|
||||||
|
caused and on any theory of liability, whether in contract, strict liability,
|
||||||
|
or tort (including negligence or otherwise) arising in any way out of the use
|
||||||
|
of this software, even if advised of the possibility of such damage.
|
||||||
|
*)
|
||||||
|
|
||||||
|
(** {1 complements to list} *)
|
||||||
|
|
||||||
|
type 'a t = 'a list
|
||||||
|
|
||||||
|
val map : ('a -> 'b) -> 'a t -> 'b t
|
||||||
|
(** Safe version of map *)
|
||||||
|
|
||||||
|
val append : 'a t -> 'a t -> 'a t
|
||||||
|
(** Safe version of append *)
|
||||||
|
|
||||||
|
val (@) : 'a t -> 'a t -> 'a t
|
||||||
|
|
||||||
|
val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
|
||||||
|
|
||||||
|
val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
|
||||||
|
|
||||||
|
val flat_map : ('a -> 'b t) -> 'a t -> 'b t
|
||||||
|
(** map and flatten at the same time (safe). Evaluation order is not guaranteed. *)
|
||||||
|
|
||||||
|
val flatten : 'a t t -> 'a t
|
||||||
|
(** Safe flatten *)
|
||||||
|
|
||||||
|
val product : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t
|
||||||
|
(** cartesian product of the two lists, with the given combinator *)
|
||||||
|
|
||||||
|
val (<*>) : ('a -> 'b) t -> 'a t -> 'b t
|
||||||
|
|
||||||
|
val (<$>) : ('a -> 'b) -> 'a t -> 'b t
|
||||||
|
|
||||||
|
val return : 'a -> 'a t
|
||||||
|
|
||||||
|
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
|
||||||
|
|
||||||
|
val range : int -> int -> int t
|
||||||
|
(** [range i j] iterates on integers from [i] to [j] included. It works
|
||||||
|
both for decreasing and increasing ranges *)
|
||||||
|
|
||||||
|
val (--) : int -> int -> int t
|
||||||
|
(** Infix alias for [range] *)
|
||||||
|
|
||||||
|
val take : int -> 'a t -> 'a t
|
||||||
|
(** take the [n] first elements, drop the rest *)
|
||||||
|
|
||||||
|
val drop : int -> 'a t -> 'a t
|
||||||
|
(** drop the [n] first elements, keep the rest *)
|
||||||
|
|
||||||
|
val split : int -> 'a t -> 'a t * 'a t
|
||||||
|
(** [split n l] returns [l1, l2] such that [l1 @ l2 = l] and
|
||||||
|
[length l1 = min (length l) n] *)
|
||||||
|
|
||||||
|
val last : int -> 'a t -> 'a t
|
||||||
|
(** [last n l] takes the last [n] elements of [l] (or less if
|
||||||
|
[l] doesn't have that many elements *)
|
||||||
|
|
||||||
|
(** {2 Conversions} *)
|
||||||
|
|
||||||
|
type 'a sequence = ('a -> unit) -> unit
|
||||||
|
type 'a gen = unit -> 'a option
|
||||||
|
type 'a printer = Buffer.t -> 'a -> unit
|
||||||
|
|
||||||
|
val to_seq : 'a t -> 'a sequence
|
||||||
|
val of_seq : 'a sequence -> 'a t
|
||||||
|
|
||||||
|
val to_gen : 'a t -> 'a gen
|
||||||
|
val of_gen : 'a gen -> 'a t
|
||||||
|
|
||||||
|
(** {2 IO} *)
|
||||||
|
|
||||||
|
val pp : ?start:string -> ?stop:string -> ?sep:string ->
|
||||||
|
'a printer -> 'a t printer
|
||||||
94
core/CCOpt.ml
Normal file
94
core/CCOpt.ml
Normal file
|
|
@ -0,0 +1,94 @@
|
||||||
|
|
||||||
|
(*
|
||||||
|
copyright (c) 2013-2014, simon cruanes
|
||||||
|
all rights reserved.
|
||||||
|
|
||||||
|
redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer. redistributions in binary
|
||||||
|
form must reproduce the above copyright notice, this list of conditions and the
|
||||||
|
following disclaimer in the documentation and/or other materials provided with
|
||||||
|
the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||||
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*)
|
||||||
|
|
||||||
|
(** {1 Options} *)
|
||||||
|
|
||||||
|
type 'a t = 'a option
|
||||||
|
|
||||||
|
let map f = function
|
||||||
|
| None -> None
|
||||||
|
| Some x -> Some (f x)
|
||||||
|
|
||||||
|
let maybe f d = function
|
||||||
|
| None -> d
|
||||||
|
| Some x -> f x
|
||||||
|
|
||||||
|
let is_some = function
|
||||||
|
| None -> false
|
||||||
|
| Some _ -> true
|
||||||
|
|
||||||
|
let compare f o1 o2 = match o1, o2 with
|
||||||
|
| None, None -> 0
|
||||||
|
| Some _, None -> 1
|
||||||
|
| None, Some _ -> -1
|
||||||
|
| Some x, Some y -> f x y
|
||||||
|
|
||||||
|
let equal f o1 o2 = match o1, o2 with
|
||||||
|
| None, None -> true
|
||||||
|
| Some _, None
|
||||||
|
| None, Some _ -> false
|
||||||
|
| Some x, Some y -> f x y
|
||||||
|
|
||||||
|
let return x = Some x
|
||||||
|
|
||||||
|
let (>>=) o f = match o with
|
||||||
|
| None -> None
|
||||||
|
| Some x -> f x
|
||||||
|
|
||||||
|
let (<*>) f x = match f, x with
|
||||||
|
| None, _
|
||||||
|
| _, None -> None
|
||||||
|
| Some f, Some x -> Some (f x)
|
||||||
|
|
||||||
|
let (<$>) = map
|
||||||
|
|
||||||
|
let map2 f o1 o2 = match o1, o2 with
|
||||||
|
| None, _
|
||||||
|
| _, None -> None
|
||||||
|
| Some x, Some y -> Some (f x y)
|
||||||
|
|
||||||
|
let to_list o = match o with
|
||||||
|
| None -> []
|
||||||
|
| Some x -> [x]
|
||||||
|
|
||||||
|
type 'a sequence = ('a -> unit) -> unit
|
||||||
|
type 'a gen = unit -> 'a option
|
||||||
|
type 'a printer = Buffer.t -> 'a -> unit
|
||||||
|
|
||||||
|
let to_gen o =
|
||||||
|
match o with
|
||||||
|
| None -> (fun () -> None)
|
||||||
|
| Some _ ->
|
||||||
|
let first = ref true in
|
||||||
|
fun () -> if !first then (first:=false; o) else None
|
||||||
|
|
||||||
|
let to_seq o k = match o with
|
||||||
|
| None -> ()
|
||||||
|
| Some x -> k x
|
||||||
|
|
||||||
|
let pp ppx buf o = match o with
|
||||||
|
| None -> Buffer.add_string buf "None"
|
||||||
|
| Some x -> Buffer.add_string buf "Some "; ppx buf x
|
||||||
66
core/CCOpt.mli
Normal file
66
core/CCOpt.mli
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
|
||||||
|
(*
|
||||||
|
copyright (c) 2013-2014, simon cruanes
|
||||||
|
all rights reserved.
|
||||||
|
|
||||||
|
redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer. redistributions in binary
|
||||||
|
form must reproduce the above copyright notice, this list of conditions and the
|
||||||
|
following disclaimer in the documentation and/or other materials provided with
|
||||||
|
the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||||
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*)
|
||||||
|
|
||||||
|
(** {1 Options} *)
|
||||||
|
|
||||||
|
type 'a t = 'a option
|
||||||
|
|
||||||
|
val map : ('a -> 'b) -> 'a t -> 'b t
|
||||||
|
|
||||||
|
val maybe : ('a -> 'b) -> 'b -> 'a t -> 'b
|
||||||
|
(** [maybe f x o] is [x] if [o] is [None], otherwise it's [f y] if [o = Some y] *)
|
||||||
|
|
||||||
|
val is_some : _ t -> bool
|
||||||
|
|
||||||
|
val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
|
||||||
|
|
||||||
|
val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
|
||||||
|
|
||||||
|
val return : 'a -> 'a t
|
||||||
|
(** Monadic return *)
|
||||||
|
|
||||||
|
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
|
||||||
|
(** Monadic bind *)
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
(** {2 Conversion and IO} *)
|
||||||
|
|
||||||
|
val to_list : 'a t -> 'a list
|
||||||
|
|
||||||
|
type 'a sequence = ('a -> unit) -> unit
|
||||||
|
type 'a gen = unit -> 'a option
|
||||||
|
type 'a printer = Buffer.t -> 'a -> unit
|
||||||
|
|
||||||
|
val to_gen : 'a t -> 'a gen
|
||||||
|
val to_seq : 'a t -> 'a sequence
|
||||||
|
|
||||||
|
val pp : 'a printer -> 'a t printer
|
||||||
|
|
||||||
56
core/CCPair.ml
Normal file
56
core/CCPair.ml
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
|
||||||
|
(*
|
||||||
|
copyright (c) 2013-2014, simon cruanes
|
||||||
|
all rights reserved.
|
||||||
|
|
||||||
|
redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer. redistributions in binary
|
||||||
|
form must reproduce the above copyright notice, this list of conditions and the
|
||||||
|
following disclaimer in the documentation and/or other materials provided with
|
||||||
|
the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||||
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*)
|
||||||
|
|
||||||
|
(** {1 Tuple Functions} *)
|
||||||
|
|
||||||
|
type ('a,'b) t = ('a * 'b)
|
||||||
|
|
||||||
|
let map1 f (x,y) = f x,y
|
||||||
|
|
||||||
|
let map2 f (x,y) = x,f y
|
||||||
|
|
||||||
|
let map f g (x,y) = f x, g y
|
||||||
|
|
||||||
|
let map_same f (x,y) = f x, f y
|
||||||
|
|
||||||
|
let swap (x,y) = y, x
|
||||||
|
|
||||||
|
let (<<<) = map1
|
||||||
|
|
||||||
|
let (>>>) = map2
|
||||||
|
|
||||||
|
let ( *** ) = map
|
||||||
|
|
||||||
|
let equal f g (x1,y1) (x2,y2) = f x1 x2 && g y1 y2
|
||||||
|
|
||||||
|
let compare f g (x1,y1) (x2,y2) =
|
||||||
|
let c = f x1 x2 in
|
||||||
|
if c <> 0 then c else g y1 y2
|
||||||
|
|
||||||
|
type 'a printer = Buffer.t -> 'a -> unit
|
||||||
|
|
||||||
|
let pp pp_x pp_y buf (x,y) =
|
||||||
|
Printf.bprintf buf "(%a, %a)" pp_x x pp_y y
|
||||||
53
core/CCPair.mli
Normal file
53
core/CCPair.mli
Normal file
|
|
@ -0,0 +1,53 @@
|
||||||
|
|
||||||
|
(*
|
||||||
|
copyright (c) 2013-2014, simon cruanes
|
||||||
|
all rights reserved.
|
||||||
|
|
||||||
|
redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer. redistributions in binary
|
||||||
|
form must reproduce the above copyright notice, this list of conditions and the
|
||||||
|
following disclaimer in the documentation and/or other materials provided with
|
||||||
|
the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||||
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*)
|
||||||
|
|
||||||
|
(** {1 Tuple Functions} *)
|
||||||
|
|
||||||
|
type ('a,'b) t = ('a * 'b)
|
||||||
|
|
||||||
|
val map1 : ('a -> 'b) -> ('a * 'c) -> ('b * 'c)
|
||||||
|
|
||||||
|
val map2 : ('a -> 'b) -> ('c * 'a) -> ('c * 'b)
|
||||||
|
|
||||||
|
val map : ('a -> 'c) -> ('b -> 'd) -> ('a * 'b) -> ('c * 'd)
|
||||||
|
|
||||||
|
val map_same : ('a -> 'b) -> ('a*'a) -> ('b*'b)
|
||||||
|
|
||||||
|
val swap : ('a * 'b) -> ('b * 'a)
|
||||||
|
|
||||||
|
val (<<<) : ('a -> 'b) -> ('a * 'c) -> ('b * 'c)
|
||||||
|
|
||||||
|
val (>>>) : ('a -> 'b) -> ('c * 'a) -> ('c * 'b)
|
||||||
|
|
||||||
|
val ( *** ) : ('a -> 'c) -> ('b -> 'd) -> ('a * 'b) -> ('c * 'd)
|
||||||
|
|
||||||
|
val equal : ('a -> 'a -> bool) -> ('b -> 'b -> bool) -> ('a * 'b) -> ('a * 'b) -> bool
|
||||||
|
|
||||||
|
val compare : ('a -> 'a -> int) -> ('b -> 'b -> int) -> ('a * 'b) -> ('a * 'b) -> int
|
||||||
|
|
||||||
|
type 'a printer = Buffer.t -> 'a -> unit
|
||||||
|
|
||||||
|
val pp : 'a printer -> 'b printer -> ('a*'b) printer
|
||||||
|
|
@ -34,7 +34,7 @@ but a simple way to print complicated values without writing a lot of code.
|
||||||
type 'a sequence = ('a -> unit) -> unit
|
type 'a sequence = ('a -> unit) -> unit
|
||||||
|
|
||||||
type 'a t = Buffer.t -> 'a -> unit
|
type 'a t = Buffer.t -> 'a -> unit
|
||||||
(** A printer for the type ['a] *)
|
(** A printer for the type ['a] *)
|
||||||
|
|
||||||
(** {2 Combinators} *)
|
(** {2 Combinators} *)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,6 @@ val quad : 'a t -> 'b t -> 'c t -> 'd t -> ('a * 'b * 'c * 'd) t
|
||||||
|
|
||||||
val map : ('a -> 'b) -> 'b t -> 'a t
|
val map : ('a -> 'b) -> 'b t -> 'a t
|
||||||
|
|
||||||
|
|
||||||
(** {2 IO} *)
|
(** {2 IO} *)
|
||||||
|
|
||||||
val output : out_channel -> 'a t -> 'a -> unit
|
val output : out_channel -> 'a t -> 'a -> unit
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue