Skip to content

Instantly share code, notes, and snippets.

@hcarty
hcarty / log.ml
Created March 13, 2012 20:05
Simple OCaml logging, conceptually modeled after Log::Log4perl's easy mode
open Batteries
module type Level_sig = sig
type t
val to_string : t -> string
val default_level : t
val compare : t -> t -> int
end
module type S = sig
@hcarty
hcarty / lwt_zmq.ml
Created April 1, 2012 23:56
Attempt at Lwt-enabled zeromq
module Lwt_socket = struct
type 'a t = {
socket : 'a ZMQ.Socket.t;
fd : Lwt_unix.file_descr;
}
let of_socket socket = {
socket;
fd = Lwt_unix.of_unix_file_descr ~blocking:false (ZMQ.Socket.get_fd socket);
}
@hcarty
hcarty / _tags
Created April 20, 2012 14:03
Lwt thread generation and consumption (now with zeromq)
<*.*>: package(lwt.unix), package(lwt.syntax), package(ZMQ), syntax(camlp4o)
@hcarty
hcarty / batBounded.ml
Created August 18, 2012 17:46
BatBounded module experiment
module O = BatOrd
type 'a bounding_f = min:'a -> max:'a -> 'a -> 'a option
let bounding_of_ord ?default_low ?default_high ord =
fun ~min ~max -> assert (ord min max != O.Gt);
fun x ->
match ord x min, ord x max with
| O.Lt, _ -> default_low
| _, O.Gt -> default_high
@hcarty
hcarty / unique.ml
Created September 15, 2012 20:02
Unique elements from a list
let unique (type s) ?(cmp = compare) (l : s list) =
let module M = struct
type t = s
let compare = cmp
end in
let module S = Set.Make(M) in
let s = List.fold_left (fun accu x -> S.add x accu) S.empty l in
S.elements s
@hcarty
hcarty / odl.ml
Created September 18, 2012 03:32
OCaml "data language" wrapper around a few basic float data types and bits from GSL
(*
Very WIP, assuming it goes anywhere.
Requires:
- Batteries
- Ulib (The stdlib extension, not the Unicode library - see oasis-db)
- gsl-ocaml
Compile with: ocamlbuild -use-ocamlfind -pkgs gsl,ulib,batteries odl.cma
*)
@hcarty
hcarty / convenience.ml
Created September 22, 2012 18:00
Convenience versions of BatBounded functions
let saturate_of_ord ~(bounds : 'a bound_t * 'a bound_t) ord =
match bounds with
| `o l, `o h
| `c l, `c h
| `o l, `c h
| `c l, `o h ->
bounding_of_ord ~default_low:l ~default_high:h ord ~bounds
|- BatOption.get
| `u, `o h
| `u, `c h ->
@hcarty
hcarty / bound.ml
Created September 24, 2012 02:16
Bounded values experiment
(* Experimenting with different ideas for convenience additions to BatBounded *)
module type S = sig
type base_t
type t
val make : base_t -> t
val bind : (base_t -> t) -> t -> t
val map2 : (base_t -> base_t -> base_t) -> t -> t -> t
end
@hcarty
hcarty / bounded_ex.ml
Created September 24, 2012 13:44
Bounded example
module B = BatBounded
(* Integers between zero and 10, inclusive *)
module I10 = struct
type base_t = int
type t = int
let bounds = `c 0, `c 10
let bounded ~bounds = B.saturate_of_ord ~bounds BatInt.ord
let base_of_t x = Some x
let base_of_t_exn = BatPervasives.identity
@hcarty
hcarty / pp_bat.ml
Created September 26, 2012 16:52
BatEnum, BatArray, BatList pretty printers
let pp_enum ?(flush = false) ?(first = "") ?(last = "") ?(sep = " ") ?(indent = String.length first) pp f e =
let open Format in
pp_open_box f indent;
pp_print_cut f ();
pp_print_string f first;
pp_print_cut f ();
match Enum.get e with
| None ->
pp_print_string f last;
pp_close_box f ();