Skip to content

Instantly share code, notes, and snippets.

@webstrand
webstrand / stdin_quine.pl
Last active November 12, 2019 14:51
A self-installing quine that can be piped to a perl binary over the network. Installs to /payload when running from STDIN, keeps STDIN open for interactivity.
#!/usr/bin/perl
BEGIN{B:{$_=<<'STRAP'; chomp; exit (eval() // exit print STDERR $@)}}
#line 4 "STDIN"
my $payload = "/tmp/payload";
last unless $0 eq "-";
my $F; do { local $^F=9999; open($F, '>', $payload) or die "$payload: $!" };
$payload //= "/proc/self/fd/" . fileno($F);
chmod(0744, $F) or die "$payload: $!";
$/="\n__END__\n";
print $F <<EVAL, scalar <STDIN> or die "$payload: $!";
@webstrand
webstrand / sipe.pl
Created November 7, 2019 17:57
A `cat`-like program which exits when STDOUT is closed. Useful for piping files followed by stdin: `sipe some_file /dev/stdin | your-program`
#!/usr/bin/perl
use strict;
use warnings;
use v5.26;
use Errno qw(EAGAIN EWOULDBLOCK EINTR);
use Fcntl qw(O_WRONLY);
STDOUT->autoflush(1);
# This is a tool like cat, except that it watches for STDOUT close and exits
# immediately.
@webstrand
webstrand / append.ts
Last active February 26, 2020 23:52
Three versions of non-recursive Append<T extends unknown[], U> (AppendV3 is recommended)
export type AppendV1<Tuple extends unknown[], Last> =
Tuple extends [infer _1] ? [_1, Last] :
Tuple extends [infer _1, infer _2] ? [_1, _2, Last] :
Tuple extends [infer _1, infer _2, infer _3] ? [_1, _2, _3, Last] :
Tuple extends [infer _1, infer _2, infer _3, infer _4] ? [_1, _2, _3, _4, Last] :
Tuple extends [infer _1, infer _2, infer _3, infer _4, infer _5] ? [_1, _2, _3, _4, _5, Last] :
Tuple extends [infer _1, infer _2, infer _3, infer _4, infer _5, infer _6] ? [_1, _2, _3, _4, _5, _6, Last] :
Tuple extends [infer _1, infer _2, infer _3, infer _4, infer _5, infer _6, infer _7] ? [_1, _2, _3, _4, _5, _6, _7, Last] :
Tuple extends [infer _1, infer _2, infer _3, infer _4, infer _5, infer _6, infer _7, infer _8] ? [_1, _2, _3, _4, _5, _6, _7, _8, Last] :
Tuple extends [infer _1, infer _2, infer _3, infer _4, infer _5, infer _6, infer _7, infer _8, infer _9] ? [_1, _2, _3, _4, _5, _6, _7, _8, _9, Last] :
@webstrand
webstrand / concat.ts
Created February 27, 2020 14:22
Concat<U extends unknown[], V extends unknown[]>
type Concat<U extends unknown[], V extends unknown[]> = {
0: V,
1: {
0: U,
1: [U[0], V[0]],
2: [U[0], V[0], V[1]],
3: [U[0], V[0], V[1], V[2]],
4: [U[0], V[0], V[1], V[2], V[3]],
5: [U[0], V[0], V[1], V[2], V[3], V[4]],
6: [U[0], V[0], V[1], V[2], V[3], V[4], V[5]],
import alias from "@rollup/plugin-alias";
import commonjs from "@rollup/plugin-commonjs";
import resolve from "@rollup/plugin-node-resolve";
import typescript from "@rollup/plugin-typescript";
import { createFilter } from "@rollup/pluginutils";
import surplusCompiler from "surplus/compiler";
function surplus({ include, exclude, sourceMap = false }) {
const filter = createFilter(include, exclude);
@webstrand
webstrand / inheritance-v-composition.js
Last active March 12, 2020 20:22
benchmarking inheritance vs composition
const Benchmark = require("benchmark");
console.log("setup");
var suite = new Benchmark.Suite();
class TestInheritance extends Set {
constructor() { super() }
op(q) { this.add(q) }
}
@webstrand
webstrand / tsconfig.d.ts
Created May 19, 2020 22:08
Typings for tsconfig.json 3.9
type BuildOptions = {
watch?: boolean;
preserveWatchOutput?: boolean;
listFiles?: boolean;
listEmittedFiles?: boolean;
pretty?: boolean;
traceResolution?: boolean;
diagnostics?: boolean;
extendedDiagnostics?: boolean;
generateCpuProfile?: string;
@webstrand
webstrand / incomplete-asserts.ts
Created August 25, 2020 13:51
incomplete work on some defined-ness assertions
type Obj = {
id?: string;
name?: string;
child: undefined | {
id?: string;
}
parent: Obj | undefined;
}
// --------- assertAllDefined
@webstrand
webstrand / autoreload.pl
Created September 13, 2020 23:02
Mostly complete script that runs and reloads an interactive command whenever files change.
#!/usr/bin/env perl
use utf8;
use warnings;
use v5.30;
use experimental qw(signatures);
use FindBin;
use Linux::Inotify2;
use Time::HiRes qw(ualarm);
unless(@ARGV) {
@webstrand
webstrand / scriptisto-typescript.ts
Last active September 23, 2020 22:14
Scriptisto example for running a typescript/node script
#!/usr/bin/env scriptisto
(await import("source-map-support")).default.install();
import * as fs from "fs";
fs.writeFileSync("/dev/stderr", "hello world!\n");
// scriptisto-begin
// script_src: main.ts
// build_once_cmd: npm install
// build_cmd: npx tsc -p .