Skip to content

Instantly share code, notes, and snippets.

@toolslive
Created October 7, 2015 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toolslive/73cf506cdea22d2630a3 to your computer and use it in GitHub Desktop.
Save toolslive/73cf506cdea22d2630a3 to your computer and use it in GitHub Desktop.
module String = struct
include String
let show (t:string) = Printf.sprintf "%S" t
let pp formatter t =
Format.pp_print_string formatter t
end
module type E = sig
type t
val compare : t -> t -> int
val show : t -> string
val pp : Format.formatter -> t -> unit
end
module Set =
struct
module Make(E:E) = struct
include Set.Make(E)
let show (t:t) =
let b = Buffer.create 128 in
let n = cardinal t in
Buffer.add_string b "{:";
let c = ref 0 in
iter
(fun x ->
Buffer.add_string b ([%show : E.t] x);
incr c;
if !c < n then Buffer.add_string b ", ";
) t;
Buffer.add_string b ":}";
Buffer.contents b
let pp formatter t =
Format.pp_print_string formatter (show t)
end
end
module StringSet = Set.Make(String)
let test empty add show (xs:'a list) =
let r = List.fold_left (fun acc x -> add x acc) empty xs in
let my_val = [r] in
Printf.printf "%s\n" (show my_val)
let ()= test
StringSet.empty
StringSet.add
[%show: StringSet.t list]
["x";"y";"z"]
module Int32Set = Set.Make(
struct include Int32
let show = to_string
let pp formatter t =
Format.pp_print_string formatter (show t)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment