Skip to content

Instantly share code, notes, and snippets.

interface Req {
imagePath?: string; // path to an image on the server. take precedence over image when both present. should be mutually exclusive.
image?: Uint8Array; // image as bytes. either imagePath or image should present. saved to fs and db.
args?: string[]; // the concatenation of imagePath args is passed to shell commands. saved in db
meta?: string; // metadata of this request, e.g., camera ID. a JSON string. saved in db.
lang?: string; // the type of the underling shell command. defaults to python for now. can be bash with a straight forward extension. can be any language to support various plugins.
}
interface Res {
outputImage?: Uint8Array; // image as bytes.
@xgfd
xgfd / cps.ss
Created September 13, 2017 13:45
lambda calculus to CPS
; exp ::= (exp exp) ; function application
; | (lambda (var) exp) ; anonymous function
; | var ; variable reference
(define (cps-convert term cont)
(match term
[`(,f ,e)
; (exp exp) => (exp-cps exp-cps cont)
(let (($f (gensym 'f))
($e (gensym 'e)))
@xgfd
xgfd / bayes ball rules.png
Last active October 19, 2023 17:58
Ten rules of Bayes ball
bayes ball rules.png
@xgfd
xgfd / Tiling A Defective Chessboard.js
Created May 6, 2016 09:41
Tiling a defective chessboard (that is a 2^n * 2^n board of squares with exactly one defective square) with triominos (L shaped object that can cover three squares of a chessboard).
'use strict';
function paint(grid, x, y, colr = nextColr()) {
grid.push([x, y, colr]);
}
// generate a random color
let colr = -1;
function nextColr() {
colr++;
return colr;
@xgfd
xgfd / num of digits of the nth fib.js
Last active April 14, 2016 14:52
About fibonacci sequence
function dgtFib(n) {
const phi = (1 + Math.sqrt(5)) / 2;
return Math.ceil(n * Math.log10(phi) - Math.log10(5) / 2);
}
function* oddGen() {
var n = 3;
while (true) {
yield n;
n += 2;
}
}
function streamfy(gen) {
var iter = gen();
function diff(S, L) {
var N = S.length,
M = L.length;
var V = [];
// virtual point (0, -1) for computing 0-path
V[1] = {
x: 0,
parent: null
};
function inc(x) {
return x & 1 ? inc(x >> 1) << 1 : x | 1;
}
function dec(x) {
return x & 1 ? x ^ 1 : dec(x >> 1) << 1 | 1;
}
function add(x, y) {
return x ? add(dec(x), inc(y)) : y;
@xgfd
xgfd / Example.js
Last active January 31, 2016 00:13
Y combinator
/**
* A so-called pseduo recursive function that calculates
* factorials with the assistance of a recursive function
* @param {function} fact, a function that recursively
* calculate factorials
*/
let f = function (fact) {
return function (n) {
return n <= 2 ? n : n * fact(n - 1);
};