Skip to content

Instantly share code, notes, and snippets.

@susisu
susisu / function.mjs
Created January 29, 2023 11:03
CloudFront Function for websites using CloudFront + S3 + Next.js static export (as of v13)
/* eslint-disable no-var, vars-on-top, no-param-reassign */
function redirect(uri) {
// remove repeated slashes
uri = uri.replace(/\/+/g, "/");
// remove trailing slash
if (uri !== "/" && uri.endsWith("/")) {
uri = uri.slice(0, -1);
}
return uri;
@susisu
susisu / brainfuck.ts
Created March 9, 2020 20:08
Type-level Brainfuck interpreter in TypeScript
// Type-level Brainfuck interpreter in TypeScript
// Copyright (c) 2020 Susisu ([email protected])
type State<P, M, I, O, R, K> = {
program: P,
memory: M,
input: I,
output: O,
return: R,
skip: K,
import * as lq from "@loquat/simple";
const key = Symbol("handle");
type PerformFunc = <U>(parser: lq.Parser<U>) => U;
export function handle<T>(func: (perform: PerformFunc) => T): lq.Parser<T> {
return new lq.StrictParser(state => {
let currentState = state;
let currentErr = lq.ParseError.unknown(state.pos);
@susisu
susisu / test.ts
Last active April 29, 2019 02:43
const ref = <T>(f: (x: T) => T) => {
const r = { val: f };
return () => r;
};
const id = <T>(x: T) => x;
// r: <T>() => { val: (x: T) => T }
const r = ref(id);
// unsafe instantiations of r
const r1 = r<number>();
const apply = <T, U>(f: (x: T) => U) => (x: T) => f(x);
const foo = <T extends { foo: number }>(x: T) => x.foo;
const foo2 = apply(foo); // : (x: {}) => number;
foo({ foo: 1, bar: 2 }); // OK
foo({}); // error
foo2({ foo: 1, bar: 2 }); // OK
foo2({}); // OK???
@susisu
susisu / test.hs
Last active January 15, 2019 08:20
-- tf-random 0.5
import System.Random.TF.Gen
-- Utility functions to choose one of splitted generators.
left g = let (g', _) = split g in g'
right g = let (_, g') = split g in g'
-- Initialize a generator (the seed is not relevant).
gen = seedTFGen (0, 0, 0, 0)
-- Let `b` be the tree position bits of a generator and `bi` the index in it. Now `b` and `bi` of
λ bin/bench ./gists.json 10000 pegjs
Node.js version: 10.9.0
JSON file: ./gists.json
Loops: 10000
pegjs: 56181.985ms
λ bin/bench ./gists.json 10000 parsimmon
Node.js version: 10.9.0
JSON file: ./gists.json
Loops: 10000
module Iterator: sig
type 'a t
type 'a result =
| Done
| Continue of 'a
val next: 'a t -> 'a result
end
interface IParser<T> { }
declare class Opt<K extends string, T> {
constructor(key: K, parser: IParser<T>);
}
declare const string: IParser<string>;
declare const number: IParser<number>;
declare class OptParser<P extends {}> {