Skip to content

Instantly share code, notes, and snippets.

Avatar
🤒
recovering from surgery (Dec 2021)

Martin Jambon mjambon

🤒
recovering from surgery (Dec 2021)
View GitHub Profile
@mjambon
mjambon / Binary.ml
Created Nov 2, 2021
Basic arithmetic operations implemented from scratch on unary and binary representations of natural numbers
View Binary.ml
(* 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 Jul 22, 2021
List.map safe reimplementation benchmark
View list_map.ml
(*
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 Jan 28, 2021
What a string library for OCaml might look like
View Lib_string.mli
(*
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 Aug 26, 2020
Delete a commit from a git branch
View git-delete
#! /usr/bin/env bash
#
# Remove a single git commit from history.
#
set -eu
usage() {
cat <<EOF
Usage: $0 <commit ID>
@mjambon
mjambon / CST.ml
Created Jul 13, 2020
Typescript CST definition with nested alternatives
View CST.ml
(* Generated by ocaml-tree-sitter. *)
(*
typescript grammar
entrypoint: program
*)
open! Sexplib.Conv
open Tree_sitter_run
@mjambon
mjambon / Boilerplate.ml
Created Jul 11, 2020
Java boilerplate with environment, no fold
View Boilerplate.ml
(**
Boilerplate to be used as a template when mapping the java CST
to another type of tree.
*)
(* Disable warnings against unused variables *)
[@@@warning "-26-27"]
(* Disable warning against unused 'rec' *)
[@@@warning "-39"]
@mjambon
mjambon / Boilerplate.ml
Created Jul 11, 2020
Java boilerplate with extra env being passed around
View Boilerplate.ml
(**
Boilerplate to be used as a template when mapping the java CST
to another type of tree.
*)
(* Disable warnings against unused variables *)
[@@@warning "-26-27"]
(* Disable warning against unused 'rec' *)
[@@@warning "-39"]
@mjambon
mjambon / save.sh
Last active Jul 6, 2020
Save all the previous copies of a config file without duplicates
View save.sh
# Bash function
#
# Usage: save foo.bar
# - creates a file like foo.bar.d41d8cd,
# - overwrites previous copy if identical,
# - preserves other previous versions with very high probability
#
save() {
file=$1
if [[ -f "$file" ]]; then
@mjambon
mjambon / typed-shell.sh
Last active Aug 20, 2020
Daydreaming about a shell syntax that embeds ocaml code and vice-versa
View typed-shell.sh
# Guiding principle: a shell is a domain-specific language for handling
# files and processes. These core features shall be served by a dedicated
# and already familiar syntax. Traditional programming control structures
# on the other hand are hard to use and limited in traditional shell
# languages. For this, we shall use a proper programming language, one that
# checks statically for basic programming mistakes, allows the use of rich
# data structures, and integrates well in large applications.
#
# Language properties:
#
@mjambon
mjambon / cmdliner_term_v1.ml
Last active Dec 7, 2019
That feeling when you realize there's no magic in Cmdliner
View cmdliner_term_v1.ml
(*
Sample command-line argument specified using Cmdliner's author's style.
*)
open Cmdliner
let seed_term =
let doc = "
Use the number $(docv) to initialize the global pseudo-random number
generator.