Skip to content

Instantly share code, notes, and snippets.

View mjambon's full-sized avatar

Martin Jambon mjambon

View GitHub Profile
@mjambon
mjambon / save.sh
Last active July 6, 2020 22:31
Save all the previous copies of a config file without duplicates
# 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 August 20, 2020 08:16
Daydreaming about a shell syntax that embeds ocaml code and vice-versa
# 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 December 7, 2019 02:55
That feeling when you realize there's no magic in Cmdliner
(*
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.
@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 / 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 / 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 / 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 / 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 / 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 / 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";;