Implementing pipes in Javascript using psudo operator overloading
JavaScript
Latest commit 50a64b5 Jun 14, 2013 @danielfewbytes danielfewbytes fixed typo
Permalink
Failed to load latest commit information.
README.md fixed typo Jun 14, 2013
jsPipe.js first commit. Jun 14, 2013

README.md

Implementing pipes in Javascript using pseudo operator overloading.

Most of the credit goes to Dr. Axel Rauschmayer that introduced me to the concept of pseudo operator overloading in Javascript in his great article about the subject.

The code it self is pretty simple (containing just 28 lines of Javascript for the actual pipe implementation).

I have used the fact that when doing operations on Objects, the method valueOf of the Object is being triggered. Using a bit of trickery, (and overwriting primitive prototypes) I was able to go as far as to make the following work and produce the expected result:

var result;
p([1,6,4,9,3])
| sort
| removeLessThenThree
| doubleAll
| print // prints [8, 12, 18]
| function (value) { result = value; }
|pe

This little trick is obviously not suitable for real usage, but it is a great example of Javascript's hidden flexibility.