Multimethods would be an alternative to this type of ad-hoc polymorphism. I've encoded immutable multimethod environments in my new library, bilby.js:
https://github.com/pufuwozu/bilby.js
Lets you write things like this:
var env = λ.environment() .method('length', λ.isArray, function(a) { return a.length; }) .method('length', λ.isString, function(s) { return s.length; }) .property('empty', function(o) { return !this.length(o); }); env.empty([]) == true; env.empty([1, 2, 3]) == false;
Multimethods would be an alternative to this type of ad-hoc polymorphism. I've encoded immutable multimethod environments in my new library, bilby.js:
https://github.com/pufuwozu/bilby.js
Lets you write things like this:
Where isArray and isString are any functions that return true/false based on the input arguments. The environment then dispatches whichever method that has a predicate return true first.