Skip to content

Instantly share code, notes, and snippets.

@aantron
aantron / take.ml
Last active March 15, 2016 22:24
let take limit html =
let spaces = Str.regexp "[ \t\r\n]+" in
let rec repeat acc n v = if n > 0 then repeat (v::acc) (n - 1) v else acc in
let filter_signals =
(0, 0)
|> Markup.transform (fun ((seen, unclosed_elements) as state) signal ->
match signal with
| `Text ss ->
@aantron
aantron / writev.c
Created November 19, 2016 22:12
Lwt_unix.writev performance measurements
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/times.h>
(* ocamlfind opt -linkpkg -package lwt.ppx -package lwt.unix -package mtime.os timing.ml && ./a.out *)
open Lwt.Infix
let f1 dir =
let s = Lwt_unix.files_of_directory dir in
let s = Lwt_stream.filter (fun s -> s <> "." && s <> "..") s in
let s = Lwt_stream.map (Filename.concat dir) s in
Lwt_stream.to_list s
@aantron
aantron / measure.ml
Created December 3, 2016 13:16
Profiling Markup.ml
(* ocamlfind opt -linkpkg -package unix -package markup measure.ml *)
let measure f =
let open Unix in
let start_times = times () in
let start_wall = gettimeofday () in
f ();
@aantron
aantron / lwt.sigs.ml
Last active May 14, 2017 08:39
The new lwt.ml, with modules folded and signatures expanded
(* OCaml promise library
* http://www.ocsigen.org/lwt
* ...
*)
(* Reading guide
... *)
@aantron
aantron / lwt.modules.ml
Created May 14, 2017 08:42
The new lwt.ml, with modules folded
(* OCaml promise library
* http://www.ocsigen.org/lwt
* ...
*)
(* Reading guide
... *)
@aantron
aantron / README.md
Created July 23, 2017 14:48
Lwt.pick/Lwt.npick callback ordering

Compile and run the program below with

ocamlfind opt -linkpkg -package lwt.unix foo.ml && ./a.out

As written with Lwt.pick, p2 is canceled first, its callback (registered by Lwt.catch) runs first, and the output is

p2's callback sees p3 pending
@aantron
aantron / asm-ordinary.s
Last active August 22, 2017 08:46
Code, data for OCaml ordinary and polymorphic variant performance comparison https://discuss.ocaml.org/t/lwt-core-refactored-and-documented-to-be-contributor-friendly/161/28
; Generated from
;
; type ordinary =
; | A | B | C | D
; | E of int | F of int | G of int | H of int
;
; let pattern_match = function
; | A -> 0
; | B -> 1
; | C -> 2
@aantron
aantron / html_page.ml
Last active October 4, 2018 17:19
TyXML to Markup.ml
(* Note: `Raw signal is not released. I originally had assert false in the cases that use it. *)
let write_html_to_channel : Pervasives.out_channel -> Tyxml.Html.doc -> unit =
fun channel page ->
let ns name = (Markup.Ns.html, name) in
let convert_attributes attributes =
attributes |> List.map ~f:begin fun attribute ->
let value =
(executable
(name foo)
(libraries bigarray))