News for hackers. Stuff that matters.
Re:Tuples in JavaScript by lmm on Monday July 29, 2013 @11:20AM (#6121621)
The main value of a tuple is as a type-safe alternative to an array/list (e.g. scala tuples don't even have named elements). This makes them a lot less useful in a language like Javascript.
Re:Tuples in JavaScript by nwhitehead on Monday July 29, 2013 @02:14PM (#6122821)
I like the "unpack" idea of using a function to name the parts of the tuple, it's elegant and fits well into JavaScript. I'm a bit less sure about exposing the "pack" method outside the constructor. That makes tuples mutable, which isn't what I normally want. Normally I want immutable collections of possibly mutable objects for my tuples. If I wanted a mutable collection I would use an array.

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.

Re:Tuples in JavaScript by Olical on Monday July 29, 2013 @02:41PM (#6122990)
So what if I made immutable. You can only set once and unpack to a function that can name the values. I could also drop the valueOf usage.

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.

Re:Tuples in JavaScript by nwhitehead on Monday July 29, 2013 @03:08PM (#6123167)
Actually thinking about it more you might want to have "Tuple" and "MutableTuple" objects. In languages that use parentheses notation for tuples you need to decide which one you want, but since you have names you can have both just with different names.
Re:Tuples in JavaScript by jayferd on Monday July 29, 2013 @03:29PM (#6123310)
An alternative would be the old lambda-calculus trick of just saving the function with values in the closure.

    function Tuple(/* args... */) {
      var args = arguments;
      this.unpack = function(fn) {
        return fn.apply(null, args);
      }
    }
Re:Tuples in JavaScript by Olical on Monday July 29, 2013 @03:51PM (#6123446)
The problem with that being that you redefine the unpack function every time a tuple is created. The this._NAME = VALUE; may look awful but it's more efficient.

You are just relying on your users to not abuse _ prefixed variables. It's far from ideal.

Re:Tuples in JavaScript by jayferd on Monday July 29, 2013 @03:58PM (#6123482)
Actually, good js implementations (like v8, for example) will only create one code object shared by all the unpack functions, with separate closures assigned to each. And since closures can (usually) be statically determined, they can use an offset lookup for the closured variable in place of a hashtable. So it's not quite as inefficient as it looks :).
Re:Tuples in JavaScript by Olical on Monday July 29, 2013 @05:20PM (#6123969)
Huh, I never knew that. Thanks! As long as you're in a decent environment that kind of definition becomes a lot smoother. I like it.
Re:Tuples in JavaScript by Olical on Monday July 29, 2013 @05:20PM (#6123974)
Streamlined, removed the insanity, tested and documented. Any better? https://github.com/Wolfy87/tuple
Re:Tuples in JavaScript by kevingadd on Monday July 29, 2013 @11:53AM (#6121850)
Why does it even have that pack method, let alone fill the array backwards? You could just do this._store = Array.prototype.slice.call(arguments). Tuple.unpack(callback) isn't any better than callback.apply(x, array), so not sure what value it offers there either. The valueOf definition is the sort of thing that makes me cringe because it would undoubtedly lead to hard-to-diagnose bugs in JS applications. Not sure what anyone would use this type for.

Cute demonstration of ways to extend JS, I guess, but please don't use anything like this code.

Re:Tuples in JavaScript by Olical on Monday July 29, 2013 @12:42PM (#6122179)
It fills the array backwards because that is the most efficient loop possible within JavaScript when the order does not matter.

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.

Re:Tuples in JavaScript by kevingadd on Monday July 29, 2013 @12:56PM (#6122285)
Adding an implicit coercion to number to a tuple type that contains multiple elements in a dynamically typed language is just a bad idea. Period.
Re:Tuples in JavaScript by Olical on Monday July 29, 2013 @01:00PM (#6122313)
Okay, that isn't integral though. This could easily be left out if desired. It's not coercing to a number either, it's simply concatenating. This would work with strings too or any other type that supports valueOf.

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.

Re:Tuples in JavaScript by mikeryan on Monday July 29, 2013 @11:41AM (#6121762)
This seems like a solution in search of a problem.
Re:Tuples in JavaScript by kazagistar on Monday July 29, 2013 @05:23PM (#6123990)
Sometimes porting features from other languages into one you are familiar with helps you get a better feel for them, even if they are useless due to not aligning with the feel/philosophy of the language at all.
Re:Tuples in JavaScript by rdudekul on Monday July 29, 2013 @01:47PM (#6122650)
"I recently read Functional JavaScript and can’t physically recommend it enough".

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.

Re:Tuples in JavaScript by binarymax on Monday July 29, 2013 @11:31AM (#6121694)
Nice use of the valueOf override. Those interested in set theory in javascript may also find Set.js handy: https://github.com/gkatsev/set.js
Re:Tuples in JavaScript by dccoolgai on Monday July 29, 2013 @11:41AM (#6121766)
Not sure what value tuples really provide in JS (as other comment pointed out, they are offer the most utility in strongly-typed languages)... but this is a neat technique nonetheless and an interesting read.
Re:Tuples in JavaScript by casual_slacker on Monday July 29, 2013 @01:52PM (#6122679)
How does this differ than using a magnetized needle and a steady hand?
Re:Tuples in JavaScript by strager on Monday July 29, 2013 @05:20PM (#6123970)
Why so much code? A closure is shorter and simpler. https://gist.github.com/strager/6107955
Re:Tuples in JavaScript by dancecodes on Monday July 29, 2013 @12:21PM (#6122047)
where is data matching operations like in Erlang?
Re:Tuples in JavaScript by pfraze on Monday July 29, 2013 @02:37PM (#6122964)
If wishing made it so.