Skip to content

Instantly share code, notes, and snippets.

@hcarty
hcarty / r_lwt.ml
Last active August 29, 2015 14:23
A small amount of Rresult + Lwt, somewhat in the style of Pvem's Lwt support
open Rresult
include R
type ('a, 'b) result_lwt = ('a, 'b) result Lwt.t
let ok x = Lwt.return @@ Ok x
let error e = Lwt.return @@ Error e
let return = ok
let bind x f =
@hcarty
hcarty / toggle_semi.ml
Created May 11, 2015 14:37
Snippet to toggle requiring/not requiring ;; in utop
(* Put this in ~/.ocamlinit to be able to toggle requiring ;; on and off *)
let toggle_semi =
let original = !UTop.parse_toplevel_phrase in
let no_semi str eos_is_error = original (str ^ ";;") eos_is_error in
let semi = ref true in
fun () ->
UTop.parse_toplevel_phrase := if !semi then no_semi else original;
semi := not !semi
;;
@hcarty
hcarty / build.txt
Created March 18, 2015 18:21
ppx_import in .ml from .mli
$ ocamlbuild -classic-display -use-ocamlfind -package ppx_import,ppx_deriving.std test.cma
ocamlfind ocamldep -package ppx_import,ppx_deriving.std -modules test.mli > test.mli.depends
ocamlfind ocamlc -c -package ppx_import,ppx_deriving.std -o test.cmi test.mli
ocamlfind ocamldep -package ppx_import,ppx_deriving.std -modules test.ml > test.ml.depends
ocamlfind ocamlc -c -package ppx_import,ppx_deriving.std -o test.cmo test.ml
+ ocamlfind ocamlc -c -package ppx_import,ppx_deriving.std -o test.cmo test.ml
File "test.ml", line 1, characters 19-25:
Error: Unbound module Test
Command exited with code 2.
@hcarty
hcarty / add-ppa.sh
Created May 22, 2014 12:58
Travis config (using PPA) for ocaml-zmq
#!/bin/sh -ex
echo 'yes' | sudo apt-add-repository ppa:chris-lea/zeromq
sudo apt-get update
sudo apt-get install libzmq3-dev
@hcarty
hcarty / batSet.ml
Created January 20, 2014 17:01
BatSet infix operator possibilities
(* In the context of BatSet and BatSet.Make *)
module Infix = struct
let ( <-- ) s x = add x s
let ( < ) a b = not (equal a b) && subset a b
let ( > ) b a = not (equal a b) && subset a b
let ( <= ) a b = subset a b
let ( >= ) b a = subset a b
let ( - ) a b = diff a b
@hcarty
hcarty / makemod.ml
Created August 29, 2013 15:18
Modules from modules
module type S = sig
type t
end
module type Sout = sig
type a
type b
type t = A of a | B of b
end
@hcarty
hcarty / indexMapGADT.ml
Created July 29, 2013 10:13
GADT-based index map experiment
type read
type write
type no
type (_, _, _) t =
| Read : ('i -> 'e) -> ('i, 'e, (read * no)) t
| Write : ('i -> 'e -> unit) -> ('i, 'e, (no * write)) t
| ReadWrite : ('i -> 'e) * ('i -> 'e -> unit) -> ('i, 'e, (read * write)) t
let make_ro get = Read get
@hcarty
hcarty / lwt_indexed_io.ml
Last active January 25, 2016 02:55
Lwt friendly mmap-like I/O
let ( >>= ) = Lwt.( >>= )
let ( >|= ) = Lwt.( >|= )
external identity : 'a -> 'a = "%identity"
module type Src_sig = sig
type t
val read_bytes : t -> size:int -> offset:int -> string option Lwt.t
val write_bytes : t -> string -> offset:int -> unit Lwt.t
@hcarty
hcarty / lwt_promise.ml
Created June 26, 2013 02:37
Playing around with Lwt + something promise-y/future-y
let ( >>= ) = Lwt.( >>= )
let promise f x =
let mbox = Lwt_mvar.create_empty () in
Lwt.async (
fun () ->
f x >>= fun y ->
Lwt_mvar.put mbox y
);
mbox
open Ocamlbuild_plugin
let () = dispatch begin function
| After_rules ->
rule "atdgen: .atd -> _t.ml*, _j.ml*, _v.ml*"
~prods:["%_t.ml";"%_t.mli";"%_j.ml";"%_j.mli";"%_v.ml";"%_v.mli";]
~dep:"%.atd"
(begin fun env build ->
let atdgen = "atdgen" in
Seq [