What the heck is a bilby? No, it's not a mythical creature that might exist in Fantasy Land. It exists here on Earth as a freaky/cute cross between a mouse and a rabbit. Nonetheless, bibly.js
library is compliant with Fantasy Land specifications.
In fact, bilby.js
is a serious functional library. As its documentation states, it is, Serious, meaning it applies category theory to enable highly abstract code. Functional, meaning it enables referentially transparent programs. Wow, that is pretty serious. The documentation located at goes on to say that it provides:
By far the most mature library that conforms to the Fantasy Land specifications for algebraic structures, Bilby.js
is a great resource for fully committing to the functional style.
Let's try an example:
// environments in bilby are immutable structure for multimethods var shapes1 = bilby.environment() // can define methods .method( 'area', // methods take a name function(a){return typeof(a) == 'rect'}, // a predicate function(a){return a.x * a.y} // and an implementation ) // and properties, like methods with predicates that always // return true .property( 'name', // takes a name 'shape'); // and a function // now we can overload it var shapes2 = shapes1 .method( 'area', function(a){return typeof(a) == 'circle'}, function(a){return a.r * a.r * Math.PI} ); var shapes3 = shapes2 .method( 'area', function(a){return typeof(a) == 'triangle'}, function(a){return a.height * a.base / 2} ); // and now we can do something like this var objs = [{type:'circle', r:5}, {type:'rect', x:2, y:3}]; var areas = objs.map(shapes3.area); // and this var totalArea = objs.map(shapes3.area).reduce(add);
This is category theory and ad-hoc polymorphism in action. Again, category theory will be covered in full in , Category Theory.
Category theory is a recently invigorated branch of mathematics that functional programmers use to maximize the abstraction and usefulness of their code. But there is a major drawback: it's difficult to conceptualize and quickly get started with.
The truth is that Bilby and Fantasy Land are really stretching the possibilities of functional programming in JavaScript. Although it's exciting to see the evolution of computer science, the world may just not be ready for the kind of hard-core functional style that Bibly and Fantasy Land are pushing.
Maybe such a grandiose library on the bleeding-edge of functional JavaScript is not our thing. After all, we set out to explore the functional techniques that complement JavaScript, not to build functional programming dogma. Let's turn our attention to another new library, Lazy.js
.