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:8972923
Created February 13, 2014 10:31
Fugly angular
<!doctype html>
<html ng-app="app">
<head>
<script defer src="bootstrap/js/jquery.2.1.0.js"></script>
<script src="angular/angular.1.2.11.min.js"></script>
</head>
<body>
<script>
var app = angular.module('app', [])
.factory('Model', function(){ return {foo: {bar: 'initial'}}; })
@pvdz
pvdz / gist:9000941
Last active August 29, 2015 13:56
Selection api why you so verbose? :(
function f_rel(el, start, end) {
// end may be < 0 and is relative to start, start must be >=0
f_abs(el, start, start + end);
}
function f_abs(el, start, end){
// end may be 0 < end < start, start must be >=0
var s = getSelection();
s.removeAllRanges();
@pvdz
pvdz / gist:9082105
Created February 18, 2014 22:49
POC, simple transformation :)
function * foo() {
a();
yield x;
b();
}
// =>
function * foo() {
var started = false;
@pvdz
pvdz / gist:9120576
Created February 20, 2014 18:50
regular-for-to-while transformation :)
function* f(){
for(var i=0;;++i) {
yield i;
}
}
// =>
function* f(){
{var i=0;while(true) {
@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;
}