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 / 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');
});
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 / 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');
@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 / babelfish.py
Last active August 29, 2015 14:07
Sublime Text 3 plugin to detect window modification (couldn't figure out how to update other window)
# put this in /Users/adam/Library/Application Support/Sublime Text 3/Packages/User/babelfish.py
import sublime, sublime_plugin
class Bablefish(sublime_plugin.EventListener):
def on_modified(self, view):
print(view.file_name(), "modified")
w = view.window()
@adamloving
adamloving / playground.swift
Created August 19, 2014 20:12
Playing around with JSON and images
import SpriteKit
var str = "Hello, playground"
var dict : [String: String] = ["hi": "there"]
let productData = "{\"data\":[{\"url\": \"http://s2.img-b.com/build.com/imagebase/resized/330x320/moenimages/moen-tl182p-65.jpg\"}]}"
var error: NSError?
@adamloving
adamloving / minecraft-profile.sh
Created August 10, 2014 01:18
fetch (verify) a minecraft profile
curl -d '{"name":"notch","agent":"minecraft"}' -H 'Content-Type: application/json' https://api.mojang.com/profiles
# http://skins.minecraft.net/MinecraftSkins/<USERNAME>.png for skin
@adamloving
adamloving / coffee-map-hack.js
Created June 30, 2014 05:53
Compile coffee script with source map at require time.
var CoffeeScript = require('coffee-script')
// CoffeeScript.register()
function requireCoffeeScript(relativePath) {
var path = require('path');
var fs = require('fs');
relativePath = path.resolve(__dirname, relativePath);
console.log('relativePath', relativePath);
@adamloving
adamloving / show-express-routes-gulp-task.js
Created June 9, 2014 18:23
Gulp task to show the routes in an express app (similar to rails rake routes).
var app = require('../server/app.js').create()
var gulp = require('gulp');
var task = require('./index');
/*
Output all the routes that the app supports
*/
gulp.task('routes', function() {
@adamloving
adamloving / array-reference.php
Created May 30, 2014 20:48
Daily PHP WTF. Adding a key to an array creates a new and different array.
<?php
$a = array();
$b = $a;
// "When using the identity operator (===), object variables are identical
// if and only if they refer to the same instance of the same class."
if ($a === $b) print "same\n";
$a['test'] = 'something';