tubes

My internet:  github / flickr / facebook / tumblr

words

Promise chains made pretty with lodash

Lodash’s partial is my new favorite toy.

When in need of a callback that requires multiple arguments, a nice pattern is predefining it with wrap or partial.

Predefining callbacks alleviates the need for inline anonymous functions, dramatically improving self documentation. Single argument callbacks produce a promise chains that are nice and neat.

Here is a sample script to retrieve a tiger from a zoo service:


'use strict';

var _ = require('lodash');
var q = require('Q');

var zooService = {
  getAnimals : function() {
    return q([
      {'name': 'tiger'},
      {'name': 'bear'}
    ]);
  }
};

var getAnimalByName = function(name, animals) {
  return _.find(animals, {'name': name});
};

var getTiger = _.partial(getAnimalByName, 'tiger');

zooService
  .getAnimals()
  .then(getTiger)
  .then(console.log);

Give it a try, copy and paste to a `tiger.js` and run:


$ npm install lodash
$ npm install q
$ node tiger.js
{ name: 'tiger' }
November 3rd 2014

git branch --delete doug/*

I branch a lot under my own namespace: image And I wanted to delete all the merged branchs. This helps:

gitdoug () {(
    # cleanup the doug/branches
    set -eux
    git checkout master
    echo "Prune origin..."
    git remote prune origin
    echo "Deleting local merged branches ..."
    git branch -a --merged | egrep "^\s*doug" | xargs -n1 git branch -d
    echo "Deleting merged branches on origin ..."
    git branch -a --merged | grep "remotes/origin/doug" | 
        cut -d"/" -f4 | xargs -I BRANCH git push origin ":doug/BRANCH"
)}
April 19th 2013

Oh python, go to hell


>>> import module
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named module
April 4th 2013