Created
July 12, 2014 20:57
-
-
Save vlastachu/4eba1e81c6de9caafa6b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// from https://news.ycombinator.com/item?id=8024116 | |
function checkType(f) { | |
return function(a) { | |
var type = f.toString().match(/\/\/(.*)\n/)[1].trim(); | |
if(type !== typeof(a)) throw new Error('Invalid type'); | |
return f(a); | |
} | |
} | |
var halve = checkType(function(n) { | |
// number | |
return n / 2; | |
}); | |
halve(4); // returns 2 | |
halve('string'); // emits error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment