Skip to content

Instantly share code, notes, and snippets.

@aprell
aprell / cr.lua
Created November 23, 2017 10:28
Carriage return demo
local function escape(code)
return string.char(27) .. string.format("[%dm", code)
end
local function brightred(str)
return escape(1) .. escape(31) .. str .. escape(0)
end
local function write(...)
for _, str in ipairs({...}) do
@aprell
aprell / data.txt
Created November 22, 2017 17:46
Reminder to strip off carriage returns
A B C
1 2 3
4 5 6
7 8 9
@aprell
aprell / Makefile
Created August 22, 2017 12:56
GNU make special target: .ONESHELL
.ONESHELL:
foo:
@foo=defined
echo $${foo:-undefined}
.PHONY: foo
@aprell
aprell / Makefile
Last active November 11, 2021 12:53
Makefile rule with multiline shell script recipe
all: foo.sh
check: foo.sh
@set -eu; \
echo "Running $<"; \
output=$$(sh $<); \
expect=42; \
if [ "$$output" != "$$expect" ]; then \
echo "*** CHECK FAILED: expected $$expect, got $$output"; \
fi
@aprell
aprell / scoped_tasks.cpp
Created August 21, 2017 17:33
Lexically scoped tasks in C++
#include <cassert>
#include <forward_list>
#include <functional>
#include <iostream>
struct Task {
using thunk = std::function<void()>;
Task(thunk f) : f(f) {}
void operator()() const { f(); }
@aprell
aprell / scoped_tasks.c
Last active August 21, 2017 17:42
Lexically scoped tasks in C
#include <alloca.h>
#include <assert.h>
#include <stdio.h>
struct task {
void (*fn)(struct task *);
void *arg, *ret;
};
struct task_node {
#ifndef BENCH_H
#define BENCH_H
#include <stdio.h>
#include "wtime.h"
#define BENCH_LOG \
"Benchmark Time Iterations\n" \
"--------------------------------------------------------------\n" \
"%-22s %9.3lf %5s %23ld\n"
@aprell
aprell / debug.c
Created May 10, 2017 17:01
A sample GDB session
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
int int_of_string(const char *s)
{
return atoi(s);
}
int main(int argc, char *argv[])
open Unix
let lsh_builtins = ["cd"; "help"; "exit"]
let print_error msg =
prerr_string "lsh: ";
prerr_endline msg
let lsh_cd args =
assert (args.(0) = "cd");
@aprell
aprell / sort.awk
Created October 25, 2016 10:13
Sort comma-separated values
#!/usr/bin/awk -f
function join(arr, sep, i) {
joined = arr[1]
for (i = 2; i <= length(arr); i++) {
joined = joined sep arr[i]
}
return joined
}