Skip to content

Instantly share code, notes, and snippets.

View pvdz's full-sized avatar

Peter van der Zee pvdz

View GitHub Profile
@pvdz
pvdz / gist:9121613
Created February 20, 2014 19:41
for-in rewrite rules.
// var version, can only have one var with a limited initializer
// can safely reuse the new var because it can't have side effects
for (var key=init in o) stmt;
// =>
var key = init, _γ_keys = [], _γ_expro = expr;
for (key in _γ_expro) _γ_keys.push(key);
while (_γ_keys.length) if (expro.hasOwnProperty(key = _γ_keys.shift())) stmt;
// non-var version, lhs can be any single expression
// we have to create our own temp var because lhs is not "safe"
@pvdz
pvdz / gist:9121615
Created February 20, 2014 19:41
for-in rewrite rules.
// var version, can only have one var with a limited initializer
// can safely reuse the new var because it can't have side effects
for (var key=init in o) stmt;
// =>
var key = init, _γ_keys = [], _γ_expro = expr;
for (key in _γ_expro) _γ_keys.push(key);
while (_γ_keys.length) if (expro.hasOwnProperty(key = _γ_keys.shift())) stmt;
// non-var version, lhs can be any single expression
// we have to create our own temp var because lhs is not "safe"
@pvdz
pvdz / gist:9890540
Created March 31, 2014 11:44
random golf example
var q = Math,
r = 2 * q.PI,
w = q.sin,
x = q.pow,
y = q.max,
Q = q.sqrt;
// =>
var q=Math,w=q.sin,x=q.pow,y=q.max,Q=q.sqrt,r=2*q.PI;
function this_tok_mustBeString(str, nextIsExpr){
if (this_tok_getLastValue() === str) return this_tok_next(nextIsExpr);
this_tok_throwSyntaxError('Expecting current value to be ['+str+'] is ['+this_tok_getLastValue()+']');
}
==>
var this_tok_returnIsYield = false;
function this_tok_mustBeString(str, nextIsExpr){
var tag = 'start';
function this_par_parseCatch(inFunction, inLoop, inSwitch, labelSet){
if (this_tok_nextPuncIfString('catch')) {
var type = this_tok_mustBeNum(0x28, false);
if (type === 13) {
if (this_par_isReservedIdentifier(false)) this_tok_throwSyntaxError('Catch scope var name is reserved');
this_tok_next(false);
} else {
this_tok_throwSyntaxError('Missing catch scope variable');
}
@pvdz
pvdz / gist:15a1765d2127b693e040
Created June 11, 2014 18:20
Silly example to add numbers to 5, before processing
var yielding = false;
function this_makeFive(n){
if (n === 3) return this_addOne(n)+1;
else if (n === 1) return this_addThree(n)+1;
else throw 'invalid input, can only hand 1 and 3';
}
function this_addThree(n){
return this_addOne(n) + 2;
}
@pvdz
pvdz / gist:1b75c28f3a5adfca11a5
Created June 11, 2014 18:21
Silly example to add numbers to 5, after processing, beautified (ugly artifacts left in intentionally because, well, idc)
var yielding = false;
function this_makeFive(n){
var step = 1;
var v1, v0;
var f1, f0;
return function(){
while (true) {
switch (step) {
case 1:
if (n === 3);
@pvdz
pvdz / gist:f33a51dc7189d98efe23
Created July 3, 2014 10:31
Right now the sub is never fired. If you uncomment the first line, both subs are fired.
// PubSub.subscribe('foo', function(name, value) {});
PubSub.publish('foo', 'bar');
PubSub.subscribe('foo', function(name, value) {});
@pvdz
pvdz / animworker.js
Last active August 29, 2015 14:07
Creating animated gif. Demonstrates web worker lag after some frames unless respawned. For other deps see https://github.com/antimatter15/jsgif
importScripts('gifencoder/LZWEncoder.js', 'gifencoder/NeuQuant.js', 'gifencoder/GIFEncoder.js');
self.onmessage = function(event) {
//console.log("message!");
//alert("message");
//self.postMessage(event.data);
var frame_index = event.data.frame_index;
var frame_length = event.data.frame_length;
var height = event.data.height;
@pvdz
pvdz / gist:3cccc4f36c8319e09bb7
Created January 29, 2015 20:18
JS1k 2015 bare shim. This is loaded in an iframe dynamically. You can opt for an empty DOM during submit, a 2d canvas, or a webgl canvas. You can also specify max canvas width/height during submit, defaults to 100%. If empty, `a`, `c`, and `g` will obviously not exist. `top.reload()` replaces the iframe containing your demo with a new instance, …
<!doctype html>
<html style="margin: 0; padding: 0; border: 0; width: 100%; height: 100%;">
<head>
<meta charset="utf-8">
</head>
<body style="margin: 0; padding: 0; border: 0; width: 100%; height: 100%;">
<canvas style="display: block; width: WIDTHpx; height: HEIGHTpx;" width="WIDTH" height="HEIGHT"></canvas>
<script>
// shim...
var a = canvas;