Skip to content

Instantly share code, notes, and snippets.

View mjambon's full-sized avatar

Martin Jambon mjambon

View GitHub Profile
@mjambon
mjambon / example.ml
Last active September 7, 2016 21:34
OCaml hack to prevent the accidental use of the built-in polymorphic comparison functions when they're not suitable
(*
Some module defining an abstract type t that does not support the built-in
polymorphic comparison functions Pervasives.compare, (=), (<) etc.
Attempts to use polymorphic comparison on objects of type t
will raise an exception, except when comparing
the same object with Pervasives.compare.
This is a hack that may or may not work depending on the OCaml compiler
or its version. Use the provided test function to check that it behaves
@mjambon
mjambon / .bashrc
Last active April 10, 2019 21:38
Bash prompt with git branch and git stash size
git_stash_size() {
n=$( (git stash list 2> /dev/null || :) | wc -l )
if [ $n -gt 0 ]; then
echo -n " +$n"
fi
}
git_branch() {
echo -n "$(git branch 2> /dev/null \
| sed -e '/^[^*]/d' -e 's/* \(.*\)/ \[\1\]/')"
@mjambon
mjambon / dotpp
Created August 21, 2016 03:53
Hack to preprocess a dot file to support long edges ----- and backward edges <----
#! /bin/bash
# Preprocess a dot file to support long edges and backward edges:
#
# Input Output
#
# a ----------> b a -> b
# a ---> b ---> c a -> b -> c
# a ----------- b a -- b
# a <--- b b -> a
@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:
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 / 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"
@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 / 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 / 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 / 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