Skip to content

Instantly share code, notes, and snippets.

View mjambon's full-sized avatar

Martin Jambon mjambon

View GitHub Profile
@mjambon
mjambon / eng.ml
Created May 20, 2011 23:15
Print a float using the engineering notation, where the exponent is a multiple of 3
(*
Print a float using the engineering notation.
The representation is approximate in the sense that it does not always allow
to recover the original float by parsing the result.
nan
inf
-inf
0
@mjambon
mjambon / genealogy.atd highlight:.ml
Created June 29, 2011 07:44
Complete example used in the atdgen tutorial. I am looking for a lighter example that allows me to illustrate the same features. Feel free to suggest one.
<doc text="Type definitions for family trees">
type tree = {
members : person list;
filiations : filiation list;
}
type filiation = {
parent : person_id;
child : person_id;
@mjambon
mjambon / quote.ml
Created September 27, 2011 23:23
Embed the contents of a file into an OCaml program.
let quote ic =
let buf = Buffer.create 1000 in
Buffer.add_string buf "\
let contents =
\"";
try
while true do
Buffer.add_string buf (String.escaped (input_line ic));
Buffer.add_char buf '\n';
done;
@mjambon
mjambon / dev_config.yml
Created October 15, 2012 23:02
elasticsearch not indexing/finding geo_point in a subfield
cluster.name: elasticsearch_dev
path.data: ./data/
path.logs: ./log/
network.host: 0.0.0.0
@mjambon
mjambon / lwt_env.ml
Created February 26, 2015 01:18
Wrapper around Lwt threads that allows carrying an environment around, transparently.
(* ocamlfind ocamlc -c env.ml -package lwt.unix *)
(*
Wrapper around Lwt threads that allows
carrying an environment around, transparently.
Such an environment would typically consist of a request ID
that we can use in logs to identify all messages relating to
the same request received by the server.
@mjambon
mjambon / missing_trace.ml
Last active August 29, 2015 14:25
Empty stack trace for BatString.split depending on I don't know what.
(*
Both calls to BatString.split and List.assoc below
raise Not_found. The stack trace shouldn't be empty
in either case, but it is empty under certain mysterious
circumstances for BatString.split.
#use "topfind";;
#require "batteries";;
*)
@mjambon
mjambon / record.ml
Created August 31, 2015 05:26
Record field disambiguation fail
type t1 = { x: int; y: int }
type t2 = { x: int; z: int }
let f () = { x = 1; y = 0 }
(* no error *)
let () =
f () |> fun x ->
ignore x.x;
ignore x.y
@mjambon
mjambon / late_init.ml
Last active October 19, 2015 18:30
Mutual module dependencies in OCaml
(*
One-time initialization.
Useful to create mutual dependencies between modules.
Usage:
(* Module A *)
let init_foo, foo = create "foo"
type json = [
| `Bool of bool
| `Int of int
| `Object of (string * json) list
]
type _ path =
| Bool : bool path
| Int : int path
| Field : string * 'a path -> 'a path
@mjambon
mjambon / rounded.ml
Last active August 16, 2016 00:52
An abstract type for reading any number into an int with atdgen
(*
An atdgen type for reading any number into an int.
`1.0` is the default output for the atd type float, which
we want to support but is not supported by the regular parser
for atd type int.
`1.0` will be read as the OCaml int `1` and written back as `1`.
The definition that goes into the .atd file is: