Skip to content

Instantly share code, notes, and snippets.

View mjambon's full-sized avatar

Martin Jambon mjambon

View GitHub Profile
@mjambon
mjambon / parallel
Last active March 28, 2024 11:09
bash: Run parallel commands and fail if any of them fails
#! /usr/bin/env bash
#
# Run parallel commands and fail if any of them fails.
#
set -eu
pids=()
for x in 1 2 3; do
@mjambon
mjambon / Tree.ml
Created June 2, 2019 00:47
OCaml functions for printing a tree in a terminal like the 'tree' command
(*
Created by Martin Jambon and placed in the Public Domain on June 1, 2019.
Print a tree or a DAG as tree, similarly to the 'tree' command.
*)
open Printf
let rec iter f = function
| [] -> ()
@mjambon
mjambon / example.ml
Created September 1, 2022 05:40
Deriving language-specific ASTs and conversions to generic AST
module Generic = struct
(* generic expr type used for matching *)
type expr =
| A
| B of expr * expr [@lang a]
| C of expr [@lang b c]
| Unsupported of string [@fallback]
end
(*** Generated from the above ***)
@mjambon
mjambon / ocaml_lwt_sample.ml
Last active May 29, 2022 19:59
OCaml/Lwt crash course. Adult supervision recommended.
(*
Interactive approach
--------------------
You can copy-paste code into `utop`, provided you load the lwt.unix
package:
#use "topfind";;
#thread;; (* only needed in the plain `ocaml` toplevel, not in `utop`. *)
#require "lwt.unix";;
@mjambon
mjambon / everything.atd
Last active February 4, 2022 09:44
Current translation from everything.atd to everything.py done by atdpy
type kind = [
| Root (* class name conflict *)
| Thing of int
| WOW <json name="wow">
| Amaze <json name="!!!"> of string list
]
type root = {
id: string;
await: bool;
@mjambon
mjambon / example.py
Created February 2, 2022 01:38
Python sum types for atdpy
from typing import List
import json
# type t = A | B of int
class T:
pass
class T_A(T):
def __repr__(self):
@mjambon
mjambon / Binary.ml
Created November 2, 2021 09:47
Basic arithmetic operations implemented from scratch on unary and binary representations of natural numbers
(* Arithmetic with Binary numbers (unsigned, arbitrary precision!) *)
type digit = Zero | One
type t = digit list
let rec eq a b =
match a, b with
| [], [] -> true
| da :: a, db :: b when da = db -> eq a b
| _ -> false
@mjambon
mjambon / list_map.ml
Last active July 22, 2021 09:21
List.map safe reimplementation benchmark
(*
Evaluate the performance of a stack-safe implementation of List.map.
Run with:
ocamlopt -o list_map unix.cmxa list_map.ml
./list_map
*)
open Printf
@mjambon
mjambon / Lib_string.mli
Last active January 28, 2021 22:22
What a string library for OCaml might look like
(*
Extra string operations for OCaml
Conventions:
- all operations are failsafe: no exceptions are raised
- negative index i is equivalent to positive index (length - i)
- from/until indicate inclusive ranges like in 'for' loops
(for i = from to until do ... done)
- does not replicate or override the standard String module
@mjambon
mjambon / git-delete
Last active August 26, 2020 17:58
Delete a commit from a git branch
#! /usr/bin/env bash
#
# Remove a single git commit from history.
#
set -eu
usage() {
cat <<EOF
Usage: $0 <commit ID>