Skip to content

Instantly share code, notes, and snippets.

View amasad's full-sized avatar
🎱
balling

Amjad Masad amasad

🎱
balling
View GitHub Profile
@amasad
amasad / mem.js
Last active August 29, 2015 14:26
test heap reclaim
function foo() {
var a = [];
for (var i = 0; i < 20000000; i++) {
a.push({});
}
}
function repeat() {
printMem('before alloc');
foo();
@amasad
amasad / csp.js
Created January 18, 2015 03:52
A contrived example to play around with js-csp in solving problems that would traditionally employ a state machine. Imagine having a math library that needs to call into an rpc system to compute math functions. The service can only handle one request pre client at a time, therefore the client needs to do the buffering. (see comments for more info)
var spawn = require('child_process').spawn;
var csp = require('js-csp');
class Math {
constructor(readyCallback) {
this._readyCallback = readyCallback;
this._worker = spawn('6to5-node', ['worker.js']);
this._worker.stderr.pipe(process.stderr);
this._reqChan = csp.chan();
/* @flow */
class Foo {
n: ?number;
test(): boolean {
var n = this.n;
if (n == null) {
return false;
} else {
/* @flow */
// This works:
class Foo {
test(n: ?number): boolean {
if (n == null) {
return false;
} else {
return n > 10;
}

What about a closure in the param list that refers to a param later in the list?

function foo(bar = function() { return x; }, x = 1) {
  return bar();
}
foo(); // throws or 1?

I'm inclined to think that's fine because it's equivalent to:

@amasad
amasad / game-of-life.rkt
Last active December 29, 2015 21:18
Conway's Game of Life
#lang racket
(require 2htdp/image
2htdp/universe)
; Cell object.
(define make-cell
(lambda (x y)
(list (list x y) #f)))
@amasad
amasad / gifbook.js
Created October 31, 2013 10:53
play gifs on facebook
(function () {
var debounce = function (fn, d) {
var t;
return function () {
if (t) clearTimeout(t);
t = setTimeout(fn, d);
};
};
var giffy = function () {
[].slice.apply(document.querySelectorAll('a')).filter(function (a) {
@amasad
amasad / cons.js
Created June 6, 2013 15:41
cons, car, and cdr using only functions in JS
function cons (a, b) {
return function (sel) {
return sel(a, b)
}
}
function car (l) {
return l(function (a, b) {
return a
})
@amasad
amasad / adam.js
Last active December 10, 2015 12:08 — forked from petermichaux/adam.js
var adam = Proxy.create({
get: function (rec, message) {
var firstname = "Adam";
var lastname = "of Eden";
switch (message) {
case "getName":
return firstname + " " + lastname;
default:
throw "unknown message " + message;
}
@amasad
amasad / router.js
Last active December 10, 2015 07:28
A quick and dirty yet powerful JS router.
// requires underscore.js for `_.clone`, although come to think about it could be done without it.
// The router module.
var router = (function () {
// Get the location array and filter out empty strings.
var path = window.location.pathname.split('/').filter(Boolean)
// The param arg stack.
, arg_stack = [];