Skip to content

Instantly share code, notes, and snippets.

@laser
laser / I.js
Created November 26, 2013 19:21
I.js
function sync(gen) {
var iterable, resume;
resume = function(err, retVal) {
if (err) iterable.raise(err); // raise!
iterable.next(retVal);
};
iterable = gen(resume);
iterable.next();
@laser
laser / J.js
Last active December 29, 2015 11:29
J.js
sync(function* (resume) {
var oneAndTwo = yield [_get("test1.txt", resume()), _get("test2.txt", resume())]
var three = yield _get("test3.txt", resume())
log(oneAndTwo[0] + " - " + oneAndTwo[1] + " " + three)
});
@laser
laser / async.js
Last active December 30, 2015 10:29
Parallel and then series, in JavaScript (Promise, async, generators, POJS)
// async
async.waterfall([
function(callback) {
async.parallel([
function(callback) {
_get("/token", callback);
},
function(callback) {
_post("/key", callback);
@laser
laser / scheduler_vanilla.js
Last active December 30, 2015 12:29
scheduler
app.get('/home', prioritize(prioritizer, responder));
var queues = [];
function flush() {
if (!flush.polling) {
flush.polling = setInterval(function() {
var x;
for (var i = 0; i < queues.length; i++) {
for (var j = 0; j < queues[i].length; j++) {
@laser
laser / _README.md
Last active December 31, 2015 11:59
Modifying sync (and consumer) to handle concurrent stuff

Some notes about the gist:

  • Calling "sync" creates an array to keep track of return values from any asynchronous operation.

  • The "resume" method is called in your generator code and its return value is passed as the callback to your asynchronous operation. Calling "resume" causes a counter to increment, letting your "sync" method know how many asynchronous operations are in flight.

  • The return value of "resume" is a function that causes the result of the asynchronous operation to be placed into the data structure in #1. Immediately after doing this assignment, we check to see if there are any outstanding asynchronous operations. If not, we resume the generator with either an (ordered) array containing the results of our asynchronous operations or (in the event of only one operation having been fired off)

The important thing to note is that both asynchronous operations have been fired off before yielding. Each callback (passed to these asynchronous calls) is the return value from the call to "resume" -

@laser
laser / concurrent_sync.js
Last active June 10, 2016 23:56
Modifying "sync" to do parallel stuff
function sync(gen) {
var iterable, resume, check, vals, ops;
vals = [];
ops = 0;
check = function() {
if (vals.length == ops) {
if (ops == 1) {
iterable.next(vals[0]);
@laser
laser / hash_fun.rb
Created January 17, 2014 16:16
Hash from array of name/value pairs with default value w/no mutation.
a = [["a", 1], ["b", 2]]
h = a.inject Hash.new(9999) do |hash, pair|
hash.merge Hash[[pair]]
end
h["a"] # 1
h["foo"] # 9999
@laser
laser / pure_fx_ruby.rb
Last active January 3, 2016 15:29
Pure functions in Ruby
class StatefulCalculator
def initialize
@total = 0
end
def add(x)
@total += x
self
end
@laser
laser / stateful_calculator.rb
Last active January 3, 2016 17:09
Simple Calculators
class StatefulCalculator
def initialize
@total = 0
end
def add(x)
@total += x
self
end
@laser
laser / stateful_calc.rb
Last active January 3, 2016 19:48
Calculators
class StatefulCalculator
def initialize
@total = 0
end
def add(x)
@total += x
self
end