Skip to content

Instantly share code, notes, and snippets.

@puffnfresh
Created October 10, 2012 02:23
Show Gist options
  • Save puffnfresh/3862789 to your computer and use it in GitHub Desktop.
Save puffnfresh/3862789 to your computer and use it in GitHub Desktop.
Safe ad-hoc polymorphism in JavaScript via bilby.js
var lib = require('./lib'),
λ = lib.method('show', lib.isNumber, function(n) {
return n.toString(2);
});
exports.showBinaryTimesTwo = λ.bind(λ.showTimesTwo)(λ);
var bilby = require('bilby');
module.exports = bilby.property('showTimesTwo', function(n) {
return this.show(n) + ' + ' + this.show(n) + ' = ' + this.show(n + n);
});
var binary = require('./binary'),
words = require('./words');
console.log(binary.showBinaryTimesTwo(1) + ' and ' + words.showWordTimesTwo(100));
var lib = require('./lib'),
λ = lib.method('show', lib.isNumber, function(n) {
return n == 0 ? 'zero' : n == 1 ? 'one' : n == 2 ? 'two' : 'some number';
});
exports.showWordTimesTwo = λ.bind(λ.showTimesTwo)(λ);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment