Skip to content

Instantly share code, notes, and snippets.

@pnegri
Created February 24, 2012 17:16
Show Gist options
  • Save pnegri/1902140 to your computer and use it in GitHub Desktop.
Save pnegri/1902140 to your computer and use it in GitHub Desktop.
Developing an Approach to Javascript Spaghetti
Fs = require 'fs'
Path = require 'path'
Flow = require 'async'
class TestingSomething
organize_basket: ( next_callback ) ->
@basket = {}
next_callback null
buy_3_apples: ( next_callback )->
@basket.apples = 0 unless @basket.apples
@basket.apples += 3
next_callback null
bake_apple_pie: ( next_callback ) ->
@basket.apples = 0 unless @basket.apples
@basket.pies = 0 unless @basket.pies
if (@basket.apples > 1 )
@basket.apples -= 2
@basket.pies += 1
else
console.log "No Apples for a Pie"
next_callback null
balance: ( next_callback ) ->
console.log @basket
next_callback null
run: () ->
that = this
for i in [1..10]
(
() ->
Flow.waterfall([
that.organize_basket.bind(this)
that.buy_3_apples.bind(this)
that.bake_apple_pie.bind(this)
that.buy_3_apples.bind(this)
that.balance.bind(this)
])
)()
obj = new TestingSomething()
obj.run()
var Flow, Fs, Path, TestingSomething, obj;
Fs = require('fs');
Path = require('path');
Flow = require('async');
TestingSomething = (function() {
function TestingSomething() {}
TestingSomething.prototype.organize_basket = function(next_callback) {
this.basket = {};
return next_callback(null);
};
TestingSomething.prototype.buy_3_apples = function(next_callback) {
if (!this.basket.apples) {
this.basket.apples = 0;
}
this.basket.apples += 3;
return next_callback(null);
};
TestingSomething.prototype.bake_apple_pie = function(next_callback) {
if (!this.basket.apples) {
this.basket.apples = 0;
}
if (!this.basket.pies) {
this.basket.pies = 0;
}
if (this.basket.apples > 1) {
this.basket.apples -= 2;
this.basket.pies += 1;
} else {
console.log("No Apples for a Pie");
}
return next_callback(null);
};
TestingSomething.prototype.balance = function(next_callback) {
console.log(this.basket);
return next_callback(null);
};
TestingSomething.prototype.run = function() {
var i, that, _results;
that = this;
_results = [];
for (i = 1; i <= 10; i++) {
_results.push((function() {
return Flow.waterfall([that.organize_basket.bind(this), that.buy_3_apples.bind(this), that.bake_apple_pie.bind(this), that.buy_3_apples.bind(this), that.balance.bind(this)]);
})());
}
return _results;
};
return TestingSomething;
})();
obj = new TestingSomething();
obj.run();
Wrong Output:
ly@boxdev:~/Desenvolvimento/crocus/util$ cf test
{ apples: 40, pies: 10 }
{ apples: 40, pies: 10 }
{ apples: 40, pies: 10 }
{ apples: 40, pies: 10 }
{ apples: 40, pies: 10 }
{ apples: 40, pies: 10 }
{ apples: 40, pies: 10 }
{ apples: 40, pies: 10 }
{ apples: 40, pies: 10 }
{ apples: 40, pies: 10 }
Expected Output:
ly@boxdev:~/Desenvolvimento/crocus/util$ cf test
{ apples: 4, pies: 1 }
{ apples: 4, pies: 1 }
{ apples: 4, pies: 1 }
{ apples: 4, pies: 1 }
{ apples: 4, pies: 1 }
{ apples: 4, pies: 1 }
{ apples: 4, pies: 1 }
{ apples: 4, pies: 1 }
{ apples: 4, pies: 1 }
{ apples: 4, pies: 1 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment