Skip to content

Instantly share code, notes, and snippets.

View mattbaker's full-sized avatar

Matt Baker mattbaker

  • New Relic
  • Portland, OR
View GitHub Profile
@mattbaker
mattbaker / object.prototype.js
Created June 14, 2011 18:54
Object.tap in Javascript
Object.prototype.tap = function(f){f.apply(this); return this;}
var x = {a:2};
x.tap(function(){console.log(this.a)}).a = 4; //Prints 2
console.assert(x.a == 4);
@mattbaker
mattbaker / README
Created December 22, 2011 06:02
SVG to PNG render with Node and D3.js
This example expects to have d3.min.js and d3.layout.min.js in the same directory as pie.js and pie_serv.js.
Run with node pie_serv.js
@mattbaker
mattbaker / jsdomtest.js
Created December 22, 2011 20:40
Build a pie chart using d3.js and send the result to stdout
/* jsdomtest.js: Build a pie chart using d3.js and send the result to stdout
* Requires d3.js and d3.layout.js in same directory
* Requires pie.js from this gist: https://gist.github.com/1509145
* ex. node jsdomtest.js > pie.svg
*/
var jsdom = require('jsdom'),
scripts = ["file://"+__dirname+"/d3.min.js",
"file://"+__dirname+"/d3.layout.min.js",
"file://"+__dirname+"/pie.js"],
@mattbaker
mattbaker / converttest.js
Created December 22, 2011 20:41
Send a PNG of a red square to STDOUT using node.js and convert
/* converttest.js: Send a PNG of a red square to STDOUT
* ex. node converttest.js > test.png
*/
var convert = require('child_process').spawn("convert", ["svg:", "png:-"]),
svgsrc = '<svg><rect height="100" width="100" style="fill:red;"/></svg>';
convert.stdout.on('data', function (data) {
process.stdout.write(data);
});
convert.stdin.write(svgsrc);
convert.stdin.end();
@mattbaker
mattbaker / thunk.js
Created January 6, 2012 17:12
Thunk in Javascript
function Thunk(fn) {
this.get = function() {
var v = fn();
this.get = function() { return v };
return v;
}
}
/*
* > var x = new Thunk(function() { console.log("working..."); return 2 * 2 * 2; })
@mattbaker
mattbaker / option.js
Last active August 29, 2015 13:55
"Wealthfront-style" Option Monad using ES6 Iterators
/*
* A Wealthfront-style Option implementation in Javascript implementing ES6 iterables
* Reference:
* http://eng.wealthfront.com/2010/05/better-option-for-java.html
*
* This only runs in current versions of Firefox.
* It's recommended you use the FF Scratchpad with the web console open to see logging results:
* https://developer.mozilla.org/en-US/docs/Tools/Scratchpad
*/
@mattbaker
mattbaker / keybase.md
Created January 12, 2015 20:14
keybase.md

Keybase proof

I hereby claim:

  • I am mattbaker on github.
  • I am mattbaker (https://keybase.io/mattbaker) on keybase.
  • I have a public key whose fingerprint is 9563 2B6B 6E59 F85F 3B61 2419 40D9 A66D EFF9 425F

To claim this, I am signing this object:

// JavaScript variables belong to one of the following scopes: global or local(function).
// Basically, any variable defined outside of a function is a global variable. Variables
// defined inside of a function are scoped narrowly to that function, and any other
// functions defined within the scope of that function (explicit use of the var keyword assumed).
var myName = "john";
var capitalizeMyName = function() {
myName = myName.substring(0).toUpperCase() + myName.slice(1);
var name = myName;
@mattbaker
mattbaker / engima-encode.racket
Last active August 29, 2015 14:16
Enigma racket encoding for @ltw
(define (encode reflector rotor-l rotor-m rotor-r letter)
(list-ref alphabet
(rotor-translate-right (rotate rotor-r 1)
(rotor-translate-right rotor-m
(rotor-translate-right rotor-l
(reflect reflector
(rotor-translate-left rotor-l
(rotor-translate-left rotor-m
(rotor-translate-left (rotate rotor-r 1)
(letter-to-index letter))))))))))
@mattbaker
mattbaker / unfuck-messages.sh
Created April 10, 2015 18:10
unfuck-messages
#!/usr/bin/env bash
killall Messages Dock
open /Applications/Messages.app