Permalink
Browse files

Remove BatPair (in Tuple), move BatMonad into BatInterfaces

  • Loading branch information...
1 parent 5c03a25 commit 53f2218ad2fa995db917020e3e1139a1c42ce86f @thelema thelema committed Dec 9, 2011
Showing with 56 additions and 198 deletions.
  1. +2 −2 src/batEnum.ml
  2. +1 −1 src/batEnum.mli
  3. +6 −0 src/batInterfaces.ml
  4. +37 −0 src/batInterfaces.mli
  5. +0 −25 src/batMonad.ml
  6. +0 −54 src/batMonad.mli
  7. +0 −53 src/batPair.ml
  8. +0 −50 src/batPair.mli
  9. +0 −2 src/batteries.ml
  10. +0 −2 src/batteries.mllib
  11. +6 −6 testsuite/test_map.ml
  12. +4 −3 testsuite/test_mappable.ml
View
@@ -608,7 +608,7 @@ let partition = switch
**)
(**Q partition
- (Q.list Q.small_int) (fun l -> let f x = x mod 2 = 1 in List.partition f l = (Enum.partition f (List.enum l) |> Pair.map List.of_enum))
+ (Q.list Q.small_int) (fun l -> let f x = x mod 2 = 1 in List.partition f l = (Enum.partition f (List.enum l) |> Tuple.Tuple2.mapn List.of_enum))
**)
let seq init f cond =
@@ -1081,7 +1081,7 @@ module type Enumerable = sig
val of_enum : 'a t -> 'a enumerable
end
-module WithMonad (Mon : BatMonad.S) =
+module WithMonad (Mon : BatInterfaces.Monad) =
struct
type 'a m = 'a Mon.m
View
@@ -662,7 +662,7 @@ val ( @// ) : ('a -> 'b option) -> 'a t -> 'b t
This module will let you use sequence and fold_monad functions over enumerations.
*)
-module WithMonad : functor (Mon : BatMonad.S) -> sig
+module WithMonad : functor (Mon : BatInterfaces.Monad) -> sig
type 'a m = 'a Mon.m
(** Type of the monadic elements. *)
View
@@ -28,3 +28,9 @@ sig
type t
val compare : t -> t -> int
end
+
+module type Monad = sig
+ type 'a m
+ val bind : 'a m -> ('a -> 'b m) -> 'b m
+ val return: 'a -> 'a m
+end
View
@@ -48,3 +48,40 @@ sig
Example: a suitable ordering function is the generic structural
comparison function {!Pervasives.compare}. *)
end
+
+(** Monads are a design pattern which may be used to enforce strong
+ functional or non-functional constraints on the manipulation of
+ resources, while remaining in the world of functional programming.
+ For instance, monads may serve to implement approximations of a
+ types-and-effects type system, to enforce functional use of arrays
+ or other mutable data structures, or to enforce the fact that only
+ files opened for writing may be actually used for writing.
+
+ For more information on monads, see
+ {{:http://enfranchisedmind.com/blog/2007/08/06/a-monad-tutorial-for-ocaml/}
+ A Monad Tutorial for Ocaml}.
+
+ This definition is compatible with the standard syntax extension
+ for monads. For more information, see
+ {{:http://www.cas.mcmaster.ca/~carette/pa_monad/} the
+ documentation of pa_monad}.
+
+ @author David Teller
+ *)
+
+(** Signature for monads *)
+module type Monad = sig
+
+ (** The type of a monad producing values of type ['a].*)
+ type 'a m
+
+ (** Monadic binding.
+
+ [bind m f] executes first [m] then [f], using the
+ result of [m]. *)
+ val bind : 'a m -> ('a -> 'b m) -> 'b m
+
+ (**Return a value, that is, put a value in the monad.*)
+ val return: 'a -> 'a m
+
+end
View
@@ -1,25 +0,0 @@
-(*
- * Monad -- Base monadic operations
- * Copyright (C) 2008 David Teller
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version,
- * with the special exception on linking described in file LICENSE.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *)
-
-module type S = sig
- type 'a m
- val bind : 'a m -> ('a -> 'b m) -> 'b m
- val return: 'a -> 'a m
-end
View
@@ -1,54 +0,0 @@
-(*
- * Monad -- Base monadic operations
- * Copyright (C) 2008 David Teller
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version,
- * with the special exception on linking described in file LICENSE.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *)
-
-(** Definition of monads.
-
- Monads are a design pattern which may be used to enforce strong
- functional or non-functional constraints on the manipulation of
- resources, while remaining in the world of functional programming.
- For instance, monads may serve to implement approximations of a
- types-and-effects type system, to enforce functional use of
- arrays or other mutable data structures, or to enforce the
- fact that only files opened for writing may be actually used
- for writing.
-
- For more information on monads, see {{:http://enfranchisedmind.com/blog/2007/08/06/a-monad-tutorial-for-ocaml/}
- A Monad Tutorial for Ocaml}.
-
- This definition is compatible with the standard syntax extension for monads.
- For more information, see {{:http://www.cas.mcmaster.ca/~carette/pa_monad/} the documentation of pa_monad}.
-
- @author David Teller
-*)
-
-(** Signature for monads *)
-module type S = sig
- type 'a m
- (** The type of a monad producing values of type ['a].*)
-
- val bind : 'a m -> ('a -> 'b m) -> 'b m
- (** Monadic binding.
-
- [bind m f] executes first [m] then [f], using the
- result of [m]. *)
-
- val return: 'a -> 'a m
- (**Return a value, that is, put a value in the monad.*)
-end
View
@@ -1,53 +0,0 @@
-(*
- * Pair - functions for pairs of values
- * Copyright (C) 2003 Nicolas Cannasse
- * 2008 David Teller (Contributor)
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version,
- * with the special exception on linking described in file LICENSE.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *)
-
-type ('a,'b) t = 'a * 'b
-
-type 'a enumerable = 'a * 'a
-type 'a mappable = 'a * 'a
-
-let map f (x,y) =
- (* force left-to-right evaluation order (this principle of least
- surprise is already applied in stdlib's List.map) *)
- let a = f x in
- (a, f y)
-
-let compare ?(c1=Pervasives.compare) ?(c2=Pervasives.compare) (a,b) (c,d) =
- let comp = c1 a c in
- if comp <> 0 then comp else c2 b d
-
-let enum (x,y) = BatList.enum [x;y] (* Make efficient? *)
-
-let of_enum e = match BatEnum.get e with
- None -> failwith "Pair.of_enum: not enough elements"
- | Some x -> match BatEnum.get e with
- None -> failwith "Pair.of_enum: not enough elements"
- | Some y -> (x,y)
-
-let print ?(first="(") ?(sep=",") ?(last=")") print_a print_b out (a,b) =
- BatIO.nwrite out first;
- print_a out a;
- BatIO.nwrite out sep;
- print_b out b;
- BatIO.nwrite out last
-
-let print2 printer out pair = print printer printer out pair
-
View
@@ -1,50 +0,0 @@
-(*
- * Pair - functions for pairs of values
- * Copyright (C) 2009 Edgar Friendly
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version,
- * with the special exception on linking described in file LICENSE.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *)
-
-(** Functions for pairs of values
-
- @author Edgar Friendly
- @deprecated Use {!Tuple.Tuple2} instead.
-*)
-
-type ('a,'b) t = 'a * 'b
-
-(** map a function across both values in a pair *)
-val map : ('a -> 'b) -> ('a * 'a) -> ('b * 'b)
-
-(** Compare two pairs in lexicographic order, possibly using custom comparators for the two fields *)
-val compare : ?c1:('a -> 'a -> int) -> ?c2:('b -> 'b -> int) -> ('a * 'b) -> ('a * 'b) -> int
-
-(** builds a two-element enum of a pair *)
-val enum : ('a * 'a) -> 'a BatEnum.t
-
-(** builds a pair out of the first two values of an enum. Raises [Failure] if insufficient elements *)
-val of_enum : 'a BatEnum.t -> ('a * 'a)
-
-(** Prints a pair using given printing functions. The parameters
- [first], [sep], and [last] default to produce "(a,b)". *)
-val print : ?first:string -> ?sep:string -> ?last:string -> ('o BatIO.output -> 'a -> unit) -> ('o BatIO.output -> 'b -> unit) -> 'o BatIO.output -> ('a * 'b) -> unit
-
-val print2 : ('o BatIO.output -> 'a -> unit) -> 'o BatIO.output -> ('a * 'a) -> unit
-
-
-include BatEnum.Enumerable with type 'a enumerable = 'a * 'a
-include BatInterfaces.Mappable with type 'a mappable = 'a * 'a
-
View
@@ -122,8 +122,6 @@ module Deque = BatDeque
module Hashcons = BatHashcons
module Heap = BatHeap
module Logger = BatLogger
-module Monad = BatMonad
-module Pair = BatPair
module ParserCo = BatParserCo
module PathGen = BatPathGen
module Print = BatPrint
View
@@ -56,13 +56,11 @@ BatInnerWeaktbl
BatInterfaces
BatLazyList
BatLogger
-BatMonad
BatMultiPMap
BatNumber
BatOption
BatOptParse
BatOrd
-BatPair
BatParserCo
BatPathGen
BatPrint
View
@@ -138,10 +138,10 @@ module TestMap
let eq_li ?msg cmp_elt print_elt l1 l2 =
let cmp t1 t2 =
- let cmp = BatPair.compare ~c1:BatInt.compare ~c2:cmp_elt in
+ let cmp = BatTuple.Tuple2.compare ~cmp1:BatInt.compare ~cmp2:cmp_elt in
0 = BatList.make_compare cmp t1 t2 in
let printer =
- BatIO.to_string -| BatList.print <| BatPair.print BatInt.print print_elt in
+ BatIO.to_string -| BatList.print <| BatTuple.Tuple2.print BatInt.print print_elt in
U.assert_equal ?msg ~cmp ~printer l1 l2
let eq ?msg cmp_elt print_elt t1 t2 =
@@ -333,7 +333,7 @@ module TestMap
| None, Some _ -> -1
| Some _, None -> 1
| Some a, Some b -> cmp a b in
- let pair_compare2 cmp = BatPair.compare ~c1:cmp ~c2:cmp in
+ let pair_compare2 cmp = BatTuple.Tuple2.compare ~cmp1:cmp ~cmp2:cmp in
eq ~msg:
"merge (fun k a b -> Some (a, b)) [0,0; 1,1; 3,3] [1,-1; 2,-2; 3,-3; 4,-4
= [0, (Some 0, None);
@@ -342,7 +342,7 @@ module TestMap
3, (Some 3, Some -3);
4, (None, Some -4)]"
(pair_compare2 (option_compare BatInt.compare))
- (BatPair.print2 (BatOption.print BatInt.print))
+ (BatTuple.Tuple2.printn (BatOption.print BatInt.print))
(M.merge (fun k a b -> Some (a, b)) t t')
(il [0, (Some 0, None);
1, (Some 1, Some ~-1);
@@ -628,10 +628,10 @@ let heterogeneous_tests =
let (@=) msg (act, exp) =
let cmp t1 t2 =
- let cmp = BatPair.compare ~c1:BatInt.compare ~c2:BatInt.compare in
+ let cmp = BatTuple.Tuple2.compare ~cmp1:BatInt.compare ~cmp2:BatInt.compare in
0 = BatList.make_compare cmp t1 t2 in
let printer =
- BatIO.to_string -| BatList.print <| BatPair.print2 BatInt.print in
+ BatIO.to_string -| BatList.print <| BatTuple.Tuple2.printn BatInt.print in
U.assert_equal ~msg ~cmp ~printer exp act in
let compare_modulo p x y = BatInt.compare (x mod p) (y mod p) in
@@ -43,13 +43,14 @@ let test_list_mappable () =
let test_array_mappable () =
let module T = TestMappable(BatArray) in
T.test_map_evaluation_order BatInt.print [|1; 2; 3|]
-
+(*
let test_pair_mappable () =
- let module T = TestMappable(BatPair) in
+ let module T = TestMappable(BatTuple.Tuple2) in
T.test_map_evaluation_order BatInt.print (1, 2)
+ *)
let tests = "Mappable" >::: [
"Array" >:: test_array_mappable;
"List" >:: test_list_mappable;
- "Pair" >:: test_pair_mappable;
+(* "Pair" >:: test_pair_mappable;*)
]

0 comments on commit 53f2218

Please sign in to comment.