Skip to content

Instantly share code, notes, and snippets.

View pdswan's full-sized avatar

Peter Swan pdswan

View GitHub Profile
@pdswan
pdswan / example.js
Last active December 15, 2015 00:09
What's the proper way to wrap a possibly asynchronous function in an angular deferred? I have an external, callback based SDK which interacts with an http API. The first time I ask for resources, an http call is made. The second time the data is cached so the callback is called immediately. This is a simplified example.
var data
function possiblyAsync(callback) {
if (alreadyLoaded) {
callback(data)
} else {
asyncLoad(function(loadedData) {
data = loadedData
callback(data)
})
}
@pdswan
pdswan / jruby_utc.rb
Created April 10, 2013 15:19
Forcing jruby to use UTC for everything
ENV['TZ'] = 'UTC'
if RUBY_PLATFORM =~ /java/
org.joda.time.DateTimeZone.setDefault(org.joda.time.DateTimeZone::UTC)
java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone('UTC'))
end
@pdswan
pdswan / purgeInbox.js
Created May 28, 2013 19:41
Purge unread Gmail messages older than x days
/**
* Removes all unread threads matching the query
* older_than:<interval> label:purge-after-<interval>
*/
var intervals = ["1d", "7d", "1m"];
function purgeAll() {
intervals.forEach(function(interval) {
var query = constructQuery(interval);
var threads = GmailApp.search(query);
Logger.log("Found " + threads.length + " matching query: " + query);
// result of `curl https://lydian.indabamusic.com/opportunities`
{
"status": "success",
"message": null,
"data": [
{
"id": "1237fa00-c24c-11e2-94e4-12313b0b1241",
"slug": "device-you-think-you-know-remix-contest",
"name": "Device - \"You Think You Know\" Remix Contest",
"tempo": "179",
@pdswan
pdswan / sequentially.js
Created June 6, 2013 16:24
semi-functional js experimentation forcing sequentially execution
var assert = require('assert')
function _if(predicate) {
return {
then: function(thenFn) {
return {
else: function(elseFn) {
predicate.map(function(val) {
if (val) thenFn()
else elseFn()
var Channels = require('./channels')
, Emitter = require('events').EventEmitter
var repeater = Channels.createRepeating(250, 'hello', 'world')
, emitter = new Emitter()
, emitterTestChannel = Channels.fromEmitter(emitter, 'test')
, mergedChannel = Channels.merge(repeater, emitterTestChannel)
, mappedChannel = Channels.map(function() {
return Array.prototype.join.call(arguments, ' ')
}, repeater)
@pdswan
pdswan / Gemfile
Created September 10, 2013 06:23
Experimentation with making a wrapper for ActiveRecord::Relation that can be used like any other enumerable. Starting with select.
source 'https://rubygems.org'
gem 'activerecord', '~> 3.2'
gem 'activerecord-mysql2-adapter'
group :test do
gem 'rspec'
gem 'pry'
end
@pdswan
pdswan / slide_10.rb
Last active December 31, 2015 04:19 — forked from anonymous/slide_10.rb
class Car
def initialize(model, engine = ::Engine.new)
@model = model
@engine = engine
end
def running
engine.running
end
@pdswan
pdswan / js.js
Created December 20, 2013 04:11
function Foo(options) {
var privateInstanceVar = options.thing;
var publicInstanceVar = options.otherThing;
var publicInterface = {
publicFunction: publicFunction,
publicInstanceVar: publicInstanceVar
};
return publicInterface;
" copy all this into a vim buffer, save it, then...
" source the file by typing :so %
" Now the vim buffer acts like a specialized application for mastering vim
" There are two queues, Study and Known. Depending how confident you feel
" about the item you are currently learning, you can move it down several
" positions, all the way to the end of the Study queue, or to the Known
" queue.
" type ,, (that's comma comma)