Tuples in JavaScript
oli.me.uk
oli.me.uk
I absolutely hate the idea of "valueOf". Why would you ever in your life want to add together tuple values by default? It's a misfeature that is begging to cause bugs. A better feature would be some way of applying functions across tuples of the same size (map and fold type operations) to make it easy to build up correct comparison functions.
Does that sound saner? It is definitely simpler and a lot closer to an actual tuple implementation.
I want to turn this into a small repository so maybe I will take the little good bits and drop anything unnecessary.
function Tuple(/* args... */) {
var args = arguments;
this.unpack = function(fn) {
return fn.apply(null, args);
}
}You are just relying on your users to not abuse _ prefixed variables. It's far from ideal.
Cute demonstration of ways to extend JS, I guess, but please don't use anything like this code.
I added a pack method to enforce some weak limiting on the size of the array. This will only allow you to set an amount of values equal to the initial values that were set.
I know you can do it with `slice` etc, but the point was to make something a little different to an array or object inspired by the things I have been learning about functional programming.
Please, by all means, treat it as a cute demonstration. That's fine. I personally could see myself using something like this in some situations as a base type to extend from for example. Maybe as a generic coordinate storage system as I have shown in the post.
It has many flaws and might not fit in with your opinions or views, but saying "don't use anything like this code" seems a bit heavy handed to me. I think it has a time and a place, no matter how minute that time or place may be.
You have valid points though. I just didn't like to think of this as something that could never be used in the real world. Or at least contain little ideas that could be used in the real world. It has at least some relevance.
I believe this is a must read book for all advanced JavaScript developers. Michael Fogus uses UnderScore.js library by Jeremy Ashkenas, to teach functional programming using JavaScript. It changed my perspective on how to create reusable javascript functions that are easy to maintain.