Skip to content

Instantly share code, notes, and snippets.

@focusaurus
focusaurus / debug.coffee
Created March 30, 2011 03:14
CoffeeScript Introspection Function
debug = (obj, seen)->
printProps = (obj)->
#Edge case to handle is [1,2,3][9] = 'foo'
#Need to factor the conditional out to check if the prop is a number less
#than the array's length
return ((if ! /^\d+$/.test prop then prop + ": " + debug(obj[prop], seen) \
else '') for prop of obj).join(', ')
seen = seen or []
if obj in seen
@focusaurus
focusaurus / default.rb
Created August 19, 2011 21:58
Chef Recipe
require 'rubygems'
require 'json'
CONF_PATH = '/tmp/clouddial_conf.json'
cookbook_file CONF_PATH do
source File.basename CONF_PATH
mode '0444'
backup false
end
@focusaurus
focusaurus / closures.coffee
Created October 15, 2011 02:01
Closures in coffeescript
makeLogger = (prefix) ->
(message) -> console.log "#{prefix.toUpperCase()}: #{message}"
fooLogger = makeLogger 'foo'
barLogger = makeLogger 'bar'
fooLogger n for n in [1..10]
barLogger n for n in [1..10]
fooLogger n for n in [11..20]
@focusaurus
focusaurus / tipper.js
Created March 14, 2012 13:22
Clearest example of closures ever?
var tipper = function (percentage) {
return function tip(total) {
return total + (total * (percentage / 100));
};
};
var generous = tipper(20);
var normal = tipper(18);
var stingy = tipper(8);
@focusaurus
focusaurus / Model.coffee
Created July 19, 2012 01:51
Add named get/set methods to backbone models
addConvenienceMethods = ->
for prop, value of this.attributes
((prop) ->
#Define a setter/getter function
this[prop] = (newValue...) ->
if newValue.length
this.set prop, newValue[0]
return this
else
return this.get prop
@focusaurus
focusaurus / gist:3165384
Created July 23, 2012 18:50
Syntax for comments/docs vs. disabling code blocks
So the question was: should programming languages use a different syntax (or character) for comments intended to be read by developer vs comments whose purpose is to disable code. For example, in my mind, these two scenarios are semantically entirely different intents:
# Add 2 px to account for border
width += 2
vs ......
#def someFunc():
# pass
@focusaurus
focusaurus / gist:3717688
Created September 13, 2012 21:12
Make backbone.js friendly for use with async.js
//Note, works with either Backbone.Model or Backbone.Collection
//Returns a function that will fetch the provided model and works with
//async's callback API
function asyncFetch(model) {
return function (callback) {
model.fetch({
success: function (model) {callback(null, model);},
error: function (model, response) {callback(response);}
});
};
@focusaurus
focusaurus / ncsend.sh
Created October 17, 2012 19:05
Transfer clipboard text from one mac to another via netcat
NCSEND_PORT=6666
ncsend() {
local DEST_HOST="${1-smair}"
pbpaste | nc "${DEST_HOST}" "${NCSEND_PORT}"
}
ncreceive() {
while true
do
nc -l "${NCSEND_PORT}" | tee /dev/tty | pbcopy
@focusaurus
focusaurus / prefs.sh
Created December 4, 2012 16:55
How I keep OSX plist prefs in my dotfiles repo
#Export like this: prefs export iterm
#import like this: prefs import iterm
prefs() {
local OP="${1}"
shift
case "${OP}" in
export|import);
;;
*)
@focusaurus
focusaurus / emitter_demo.js
Created December 6, 2012 20:30
Emit events directly from a node module while also exporting functions
var emitterModule = require('./emitter_module');
emitterModule.on("e1", function () {
console.log("emitterModule.e1 handler invoked", arguments);
});
emitterModule.func1();