Skip to content

Instantly share code, notes, and snippets.

View briancavalier's full-sized avatar

Brian Cavalier briancavalier

  • Pittsburgh
View GitHub Profile
@briancavalier
briancavalier / filter-map-reduce-5k.txt
Created March 8, 2015 23:55
most.js filter-map-reduce perf test @ 5k events
test/perf❯ npm ls
[email protected] /Users/brian/Projects/cujojs/most/test/perf
├── [email protected]
├─┬ [email protected] (git+https://github.com/bestiejs/benchmark.js#1e8e5db13ed7a1c9c20a246a95b8fc474173c754)
│ ├── [email protected]
│ └── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]
var most = require('most');
var widgets = most.from([
{ id: 1, widgetId: 'nackjicholson-1', owner: 'nackjicholson' },
{ id: 2, widgetId: 'nackjicholson-2', owner: 'nackjicholson' },
{ id: 3, widgetId: 'nackjicholson-3', owner: 'nackjicholson' },
{ id: 4, widgetId: 'nackjicholson-4', owner: 'nackjicholson' },
{ id: 5, widgetId: 'nackjicholson-5', owner: 'nackjicholson' }
]);
@briancavalier
briancavalier / serialize-function.js
Created February 10, 2015 18:34
Serializing / deserializing JavaScript functions *with free variables*
var a = 123, b = 'hello';
function test(x, y) {
console.log(this);
return a + x + b + y;
}
// Serialize a function *with its captured environment*
var sf = serialize(test, { a: a, b: b });
// Deserialize with captured environment

Converted from http://plnkr.co/edit/kHrrvjSVTjIaiZpPdbbG?p=preview

See main.js for two ways to do it. To run:

  1. clone the gist into a web servable dir
  2. npm install
  3. open index.html (use http:// not file://)

Note: Although scheduling is configurable in most.js, we haven't yet exposed an API for it. In this case, it's simple enough just to use requestAnimationFrame directly, since it's only being used at the render boundary.

@briancavalier
briancavalier / list.js
Last active August 29, 2015 14:13
Immutable Queue
exports.list = list;
exports.of = listOf;
exports.empty = empty;
exports.cons = cons;
exports.head = head;
exports.tail = tail;
exports.reverse = reverse;
exports.foldl = foldl;
exports.foldr = foldr;
exports.concat = concat;
@briancavalier
briancavalier / withSourceMaps.js
Last active January 5, 2016 07:40
Helper for simple support for source maps in when.js unhandled rejection reporting
var when = require('when');
unhandledRejectionsWithSourceMaps(when.Promise);
function unhandledRejectionsWithSourceMaps(Promise) {
Promise.onPotentiallyUnhandledRejection = function(r) {
setTimeout(function() {
if(!r.handled) {
throw r.value;
}
var Promise = require('when').Promise;
var i = 0;
// Recursively create an infinite chain of promises
next();
function next() {
return new Promise(function (resolve) {
i++;
var Promise = require('when/lib/Promise');
var makeAbortablePromise = require('when/lib/makeAbortablePromise');
var AbortablePromise = makeAbortablePromise(Promise);
var p = new AbortablePromise(function() {
var t = setInterval(function() {
console.log('running');
}, 100);
var most = require('../most');
// A stream of periodic events
var s = most.periodic(100);
// A "timespan"
var timespan = most.of(most.of().delay(1000)).delay(5000);
// Log only events within the timespace
within(timespan, s).observe(console.log);
@briancavalier
briancavalier / iterable-test.js
Created December 11, 2014 13:38
Test program for iterable support in when.all & when/es6-shim/Promise.all
var when = require('when');
function ArrayIterable(a) {
this.array = a;
}
ArrayIterable.prototype._es6shim_iterator_ = function() {
return new ArrayIterator(this.array);
};