Skip to content

Instantly share code, notes, and snippets.

View adamloving's full-sized avatar
💭
10x Ninja Rockstar

Adam Loving adamloving

💭
10x Ninja Rockstar
View GitHub Profile
@adamloving
adamloving / functional.coffee
Created November 25, 2014 01:29
trying to come up with a simple example of why I prefer OO over functional programming
action = curry(goUp, 'hill')
[jack, jill].map(action)
for person in [jack, jill]
person.goUp('hill')
@adamloving
adamloving / next-tick.js
Created November 30, 2014 00:58
node process.nextTick()
function doInner(i) {
console.log(i, 'doInner');
process.nextTick(function() {
// runs after everything else is done
console.log(i, 'nextTick');
});
}
function doOuter(i) {
console.log(i, 'doOuter before inner');
var EventEmitter = require('events').EventEmitter;
// Constructor emits before client subscribed
function StreamLibrary(resourceName) {
// should use process.nextTick here to delay the firing of the event
this.emit('start');
// read from the file, and for every chunk read, do:
this.emit('data', chunkRead);
}
StreamLibrary.prototype.__proto__ = EventEmitter.prototype; // inherit from EventEmitter
@adamloving
adamloving / synchronous-callback.js
Created December 2, 2014 19:03
Another process.nextTick example that shows callbacks aren't intrinically asynchronous.
function doSomething(i, callback) {
console.log(i, 'doSomething before callback');
// this is in fact synchronous, since don't use nextTick
callback();
console.log(i, 'doSomething after callback');
process.nextTick(function() {
// you might think this yeilds to the next "doSomething",
// but in fact it runs after everything else is done
console.log(i, 'nextTick');
});
@adamloving
adamloving / string-compare.js
Last active August 29, 2015 14:10
javascript string compare.
if (1 == '1') console.log('1. 1 == "1"');
if (1 === '1') {} else console.log('2. 1 is not === "1" (types different)');
var a = 'a';
var a1 = a;
var a2 = 'a';
if (a == a1) console.log('3. a == a1');
if (a == a2) console.log('4. a == a2');
if (a === a2) console.log('5. even a ==== a2 (equal value and type)');
@adamloving
adamloving / confusing-sort.js
Created December 2, 2014 22:44
more confusing sorting
var items = ['0', '000', 0, 1, '001', 'a', 'z', 'A', 'Z', '!', null, undefined];
console.log('sorted', items.sort());
// defaults to string sort null becomes 'null'
// sorted [ '!', '0', 0, '000', '001', 1, 'A', 'Z', 'a', null, 'z', undefined ]
console.log('string sort', items.sort(function(a, b) {
if (a < b) {
console.log(typeof(a), a, '<', typeof(b), b, '== true');
@adamloving
adamloving / streamline-example._coffee
Created December 13, 2014 03:56
my first streamline experiment
#!/usr/bin/env _coffee
console.log 'hello ...'
setTimeout _, 100
console.log '... world'
doSomething = (isError, cb) ->
process.nextTick ->
if isError
cb(new Error('error'), isError)
else
@adamloving
adamloving / output.txt
Created December 29, 2014 02:28
nupic nlp experiment output
#COUNT TERM ONE TERM TWO TERM THREE |TERM THREE PREDICTION
-----------------------------------------------------------------------------
# 1 aunt likes baking | programming
# 2 aunt likes piano | baking
# 3 aunt eats vegetables |
# 4 uncle eats chocolate | vegetables
# 5 uncle likes bikes | programming
# 6 uncle eats coffee | chocolate
# 7 uncle likes running | bikes
# 8 uncle eats cereal | coffee
@adamloving
adamloving / terms.txt
Created December 29, 2014 02:32
more experimental nupic output
#COUNT TERM ONE TERM TWO TERM THREE |TERM THREE PREDICTION
-----------------------------------------------------------------------------
# 1 fish showed fish | heart
# 2 ignite showed fire | fish
# 3 ignite showed match | fire
# 4 grain showed wheat | fire
# 5 development showed construction | fish
# 6 development showed code | construction
# 7 labor showed worker | fire
# 8 influence showed speaker | fire
@adamloving
adamloving / phantom_shooter.js
Created December 30, 2014 00:44
An example of how to take a screenshot using phantom js.
#!/usr/bin/env node
'use strict';
var when = require('when');
var phantom = require('phantom');
exports.takeShot = function takeShot(url, outputFilePath) {
return when.promise(function(resolve, reject) {