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 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 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 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 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 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 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.
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.
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.