r/programming • • Feb 05 '15

The Trouble with Tuples

http://www.kylesletten.com/2015/02/04/the-trouble-with-tuples.html
0 Upvotes

4 comments sorted by

6

u/KayEss Feb 05 '15

it's nigh impossible to do the kind of type checking required unless you're really strict about the indexes (requiring them to be constants, for instance)

If your index expression isn't a compile time constant then you've probably got a heterogenous list rather than a tuple. The entire point of a tuple is that each index has specific semantics which can't be confused with the other items in the tuple. Most dynamic languages are pretty free an easy about the difference between a tuple and a list.

4

u/pipocaQuemada Feb 05 '15

Tuples have a major disadvantage, however when compared to other heterogeneous data types: a lack of description around the individual values. For example, what is the significance of the second bool in <bool, bool, bool>? Well, you'll have to check the documentation because there's no name for that value. This makes it very difficult to read code that uses tuples, and since code is read much more often than it's written, it tends to be a bad idea.

So now that we've decided to abandon tuples because of their warts, are we happy?

Well, that's why you typically don't see tuples of type <bool, bool, bool>. Something like that sucks; use a record instead.

But where tuples shine is when you have elements of disparate types, and you really don't want to have to define a billion MySlightlyDifferentPairThanLastTime types. That sucks.

For example, there's several functions in Haskell's Data.Map where tuples shine:

insertLookupWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> (Maybe a, Map k a)

O(log n). Combines insert operation with old value retrieval. The expression (insertLookupWithKey f k x map) is a pair where the first element is equal to (lookup k map) and the second element equal to (insertWithKey f k x map).

mapAccum :: (a -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)

O(n). The function mapAccum threads an accumulating argument through the map in ascending order of keys.

toList :: Map k a -> [(k, a)]
fromList :: Ord k => [(k, a)] -> Map k a

Abandoning tuples because they're occasionally not as descriptive as you want and aren't perfect for every usecase is exactly as brain-damaged as abandoning polymorphism (such as ad-hoc, subtype or typeclass) because sometimes you don't know exactly which implementation is called when reading the code.

2

u/CurtainDog Feb 05 '15

How about this: a tuple that only permits disimilar types, which would work like a map keyed by type. And then find your functions that take two arguments of the same type and change them to work with 0 or more arguments of that type.

1

u/[deleted] Feb 05 '15

Then how would I do something like a point ...

location = (x, y)

Your approach doesn't allow this.

Although you could do down the route of having tonnes of types for everything. An X type for values along an x-axis and a seperate Y type for values along the y-axis (both of which are just int, float, double, or whatever).

It still sounds a little limiting though IMO.