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 / 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 / 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 / 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)))

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:

/* @flow */
// This works:
class Foo {
test(n: ?number): boolean {
if (n == null) {
return false;
} else {
return n > 10;
}
/* @flow */
class Foo {
n: ?number;
test(): boolean {
var n = this.n;
if (n == null) {
return false;
} else {
@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();
@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 / babel-heap-test.js
Last active August 29, 2015 14:26
Not reclaiming unused heap
console.log('pid', process.pid);
var babel = require('babel-core');
var bigFile = require('fs').readFileSync(
require.resolve('babel-core/browser'),
'utf8'
);
babel.transform(bigFile);
console.log('before gc');
document.write('<link rel="stylesheet" href="https://assets-cdn.github.com/assets/gist/embed-e75a8866f2a8f51e8bbbf8ca746676f1e7cacb08f4b1d9b52ea62524b333cc6d.css">')
document.write('<div id=\"gist28246440\" class=\"gist\">\n <div class=\"gist-file\">\n <div class=\"gist-data\">\n <div class=\"js-gist-file-update-container js-task-list-container file-box\">\n <div id=\"file-clowtown\" class=\"file\">\n \n\n <div class=\"blob-wrapper data type-text\">\n <table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n <tr>\n <td id=\"file-clowtown-L1\" class=\"blob-num js-line-number\" data-line-number=\"1\"><\/td>\n <td id=\"file-clowtown-LC1\" class=\"blob-code blob-code-inner js-file-line\">clowntown<\/td>\n <\/tr>\n<\/table>\n\n <\/div>\n\n <\/div>\n \n<\/div>\n\n <\/div>\n <div class=\"gist-meta\">\n <a href=\"https://gist.github.com/amasad/dbbfee7b9aa8dfb55bd4/raw/e6503803eedad49fe9bb0490a4ff6f1c81060947/clowtown\" style=