Skip to content

Instantly share code, notes, and snippets.

@RasPhilCo
Last active November 25, 2015 17:56
Show Gist options
  • Save RasPhilCo/8477434123fa6dd1e6fc to your computer and use it in GitHub Desktop.
Save RasPhilCo/8477434123fa6dd1e6fc to your computer and use it in GitHub Desktop.
JavaScript Nothingness to Generators in < 200 Lines
// Welcome! We're going to go from nothingness to JS generators in 200 lines.
// But first, some vocab:
// Let's use the word chore instead of function and
// let's think of a chore as a "to-do" list of instruction(s)
// We'll use the "function" syntax to define a chore's to-do instruction(s),
// including input(s) and expected ouput, if any
// Note, "return" basically says, "quit doing stuff (even if you have more to do) and output something"
// Lastly, var is a way to give bits of code a name
// so we can use it later w/o having to rewrite the code
// If it helps, think: "input -> to-do's -> output"
// The Do Nothing Function
// returns undefined
var choreDoNothingness = function(){};
console.log(choreDoNothingness()); // undefined
// (function(){})(); <- ultimate do nothingness
// do nothing, prettier
var chorePrettierDoNothingness = function (){
return // calling return here with nothing after it
// is the same as the empty function above,
// i.e. it's reporting but there is "nothing to report on"
};
console.log(chorePrettierDoNothingness()); // undefined
// do next-to-nothing: input->undefined
var choreNextToNothing = function (x){
return
};
console.log(choreNextToNothing(1)); // undefined
// simplist Input->Output
var choreSimpilistIO = function(x){
// return (x); // another way to write it
return x;
};
console.log(choreSimpilistIO(1)); // 1
// the only "to-do's" here is pass input to output, what a lazy chore, eh?!
// simplist input->do something(add one)->output
var choreAddOne = function(x){
return x + 1
};
console.log(choreAddOne(1)); // 2
// more flexible incrementing chore
var choreIncrementBySomeValue = function(x,y){
return x + y
};
console.log(choreIncrementBySomeValue(4,5)); // 9
// input->do something(comparison)->output
// we are coupled to 1 for incrementing
var choreIncrementByOneOrDont = function(x,c){
if (x < c) {return x}
else { return x + 1 }
};
console.log(choreIncrementByOneOrDont(4,7)); // 4
console.log(choreIncrementByOneOrDont(4,2)); // 5
// decoubled, by injecting incrementation value
var choreIncrementBySomeValueOrDont = function(x,c,y){
if (x < c) {return x}
else { return x + y }
};
console.log(choreIncrementBySomeValueOrDont(4,7,5)); // 4
console.log(choreIncrementBySomeValueOrDont(4,2,5)); // 9
// doing a chore inside a chore
// now we're coupled to choreIncrement
var choreIncrementByChoreAndValueOrDont = function(x,c,y){
if (x < c) {return x}
else { return choreIncrement(x,y)}
};
console.log(choreIncrementByChoreAndValueOrDont(4,2,5)); // 9
// decoupled (or less coupled)
// but starting to get a little funky though with arguments
// still coupled to a chore with 2 arguments
// and the chore is not quite a callback chore, but we're getting somewhere
var choreIncrementBySomeChoreAndValueOrDont = function(x,c,y,incr){
if (x < c) {return x}
else { return incr(x,y)}
};
console.log(choreIncrementBySomeChoreAndValueOrDont(4,2,5,choreIncrement)); // 9
// Callbacks
// callbacks return either an error OR something
console.log("\nCallbacks...")
var choreIncrementBySomethingOrErrorThenRunCallbackChore = function(x,c,y,cbk){
if (x < c) { cbk("Error!",x)}
return cbk(false,x + y)
};
console.log(choreIncrementBySomethingOrErrorThenRunCallbackChore(4,2,5,function(err, data){
if (err) { throw err }
return ("Value is : " + data.toString())
})); // "Value is : 9"
console.log(choreIncrementBySomethingOrErrorThenRunCallbackChore(4,7,5,function(err, data){
// if (err) { throw err } // uncomment this line to see it, then comment it back out to continue
// because it's halting the page for running
return ("Value is : " + data.toString())
})); // Error!
// console.log(choreIncrementBySomethingOrErrorThenRunCallbackChore(4,7,5)); // TypeError: cbk is not a function!
// Whew! Those chore names were getting long... let's do more better...
// Closures
// closures workflow: instatiate(load up) -> input(call) -> do something -> output
// add instatiate itself is:
// input -> do something -> output a chore (function) to be called later
console.log("\nClosures...")
// not a closure yet, but an obtusely written chore
function choreAddSomethingToSomething(x,y) {
return function(y) {
return x + y;
}(y); //<-- notice (y) at the end, we're defining then immediately doing (calling) the 'anonymous' chore
}
console.log(choreAddSomethingToSomething(4,5)); //9
function choreAddSomethingToSomethingElseLater(x) {
return function(y) {
return x + y;
};
}
var add5 = choreAddSomethingToSomethingElseLater(5);
console.log(add5); // function...
console.log(add5(2)); // 7
var add10 = choreAddSomethingToSomethingElseLater(10);
console.log(add10); // function...
console.log(add10(2)); // 12
// Generators
// coming soon...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment