Skip to content

Instantly share code, notes, and snippets.

View mjambon's full-sized avatar

Martin Jambon mjambon

View GitHub Profile
@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 / .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 / 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 / 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 / bash-scoping
Last active July 19, 2018 18:17
Scoping of bash functions
#! /bin/bash
f() {
echo "$x"
}
main() {
local x=42
f
}
@mjambon
mjambon / bash-scoping2
Created July 19, 2018 17:38
Bash scoping. Does this program print 1, 2 or 3?
#! /bin/bash
x=1
f() {
echo "$x"
}
g() {
local x=2
@mjambon
mjambon / subshell
Created July 19, 2018 18:02
Subshells. Is the output 00, 11, 01, or 10?
#! /bin/bash
x=0
y=0
read x <<EOF
1
EOF
echo 1 | read y
@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 / self-extend
Last active September 19, 2018 20:14
Editing a bash script while it's running
#! /bin/bash
#
# Illustrate what happens when editing a script while it's running.
#
# This happens when saving the file in emacs, for example, but not with
# 'sed -i' because the latter deletes the old file before writing a new one.
# See https://unix.stackexchange.com/a/88572/46435 for an explanation.
#
set -eu
@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
| [] -> ()