tuple

In C++, can you have a function that modifies a tuple of variable length?

In C++0x I would like to write a function like this: template <typename... Types> void fun(typename std::tuple<Types...> my_tuple) { //Put things into the tuple } I first tried to use a for loop on int i and then do: get<i>(my_tuple); And then store some value in the result. However, get only works on constexpr. If I could get...

C# Syntax - Your preferred practice for getting 2 or 3 answers from a method

I'm just wondering how other developers tackle this issue of getting 2 or 3 answers from a method. 1) return a object[] 2) return a custom class 3) use an out or ref keyword on multiple variables 4) write or borrow (F#) a simple Tuple<> generic class http://slideguitarist.blogspot.com/2008/02/whats-f-tuple.html I'm working on some cod...

Where do theses values come from in this haskell function?

I my last question about haskell, the answer provided gave me another doubt... Instead of using the somewhat complex function in my other question, here's a simpler example: sumAll :: [(Int,Int)] -> Int sumAll xs = foldr (+) 0 (map f xs) where f (x,y) = x+y Calling sumAll [(1,1),(2,2),(3,3)] the output will be 12. What I don't unde...

Ignore python multiple return value

Say I have a Python function that returns multiple values in a tuple: def func(): return 1, 2 Is there a nice way to ignore one of the results rather than just assigning to a temporary variable? Say if I was only interested in the first value, is there a better way than this: x, temp = func() ...

Should Tuples Subclass Each Other?

Given a set of tuple classes in an OOP language: Pair, Triple and Quad, should Triple subclass Pair, and Quad subclass Triple? The issue, as I see it, is whether a Triple should be substitutable as a Pair, and likewise Quad for Triple or Pair. Whether Triple is also a Pair and Quad is also a Triple and a Pair. In one context, such a r...

Unpacking tuple types in Scala

I was just wondering, can I decompose a tuple type into its components' types in Scala? I mean, something like this trait Container { type Element } trait AssociativeContainer extends Container { type Element <: (Unit, Unit) def get(x : Element#First) : Element#Second } ...

C++0x, How do I expand a tuple into variadic template function arguments?

Consider the case of a templated function with variadic template arguments: template<typename Tret, typename... T> Tret func(const T&... t); Now, I have a tuple t of values. How do I call func() using the tuple values as arguments? I've read about the bind() function object, with call() function, and also the apply() function in diffe...

Populate a list in python

I have a series of Python tuples representing coordinates: tuples = [(1,1), (0,1), (1,0), (0,0), (2,1)] I want to create the following list: l = [] for t in tuples: l[ t[0] ][ t[1] ] = something I get an IndexError: list index out of range. My background is in PHP and I expected that in Python you can create lists that start wit...

Comparing Tuples in SQL

Is there any more convenient way to compare a tuple of data in T-SQL than doing something like this: SELECT TOP 100 A, B FROM MyTable WHERE (A > @A OR (A = @A AND B > @B)) ORDER BY A, B Basically I'm looking for rows with (A, B) > (@A, @B) (the same ordering as I have in the order by clause). I have cases where I have 3 fields, but it...

Domain and Tuple relational calculus

Is there a "real" difference between the above two? Other than the tiniest minute difference between their syntax? ...

Is there a high-level way to read in lines from an output file and have the types recognized by the structure of the contents?

Suppose I have an output file that I want to read and each line was created by joining several types together, prepending and appending the list braces, [('tupleValueA','tupleValueB'), 'someString', ('anotherTupleA','anotherTupleB')] I want to read the lines in. Now I can read them in, and operate on the string to assign values and ...

Retrieving a tuple from a collection of tuples based on a contained value

I have a data structure which is a collection of tuples like this: things = ( (123, 1, "Floogle"), (154, 33, "Blurgle"), (156, 55, "Blarg") ) The first and third elements are each unique to the collection. What I want to do is retrieve a specific tuple by referring to the third value, eg: >>> my_thing = things.get( value(3) == "Blur...

(re)Using dictionaries in django views

Hello I have this dictionary in my apps model file: TYPE_DICT = ( ("1", "Shopping list"), ("2", "Gift Wishlist"), ("3", "test list type"), ) model, which uses this dict is this: class List(models.Model): user = models.ForeignKey(User) name = models.CharField(max_length=200) type = models.PositiveIntegerFi...

newbie question: why are python strings and tuples are made immutable?

a newbie question. i am not sure why strings and tuples were made to be immutable; what are the advantages and disadvantage of making them immutable? please be gentle as it is a newbie question ;-) thanks & wish you a good day!! ...

C++: Element types in a tuple

std::pair has the nested typedefs first_type and second_type which give the type of the first and second element respectively. But is there any way to statically determine the type of the Nth element in a boost::tuple (or std::tuple in C++0x)? I know I could create my own template with N as a parameter, and use it to recursively traver...

Java: Data structure for caching computation result?

I have an expensive computation, the result of which I'd like to cache. Is there some way to make a map with two keys? I'm thinking of something like Map<(Thing1, Thing2), Integer>. Then I could check: if (! cache.contains(thing1, thing2)) { return computeResult(); } else { return cache.getValue(thing1, thing2); } pseudocode. But...

Subclassing Python tuple with multiple __init__ arguments

The following code works: class Foo(tuple): def __init__(self, b): super(Foo, self).__init__(tuple(b)) if __name__ == '__main__': print Foo([3, 4]) $ python play.py play.py:4: DeprecationWarning: object.__init__() takes no parameters super(Foo, self).__init__(tuple(b)) (3, 4) But not the following: class Foo(tup...

How do I know what data type to use in Python?

I'm working through some tutorials on Python and am at a position where I am trying to decide what data type/structure to use in a certain situation. I'm not clear on the differences between arrays, lists, dictionaries and tuples. How do you decide which one is appropriate - my current understanding doesn't let me distinguish between t...

How does indexing a list with a tuple work?

I am learning Python and came across this example: W = ((0,1,2),(3,4,5),(0,4,8),(2,4,6)) b = ['a','b','c','d','e','f','g','h','i'] for row in W: print b[row[0]], b[row[1]], b[row[2]] which prints: a b c d e f a e i c e g I am trying to figure out why! I get that for example the first time thru the expanded version is: print...

Tuple parameter declaration and assignment oddity

I can assign a tuple as follows: var (min, max) = (1, 2) But I cannot then re-assign as follows (min, max) = (1, 3) //compiler error: ';' expected but '=' found Instead I seem to have to do: min = 1 max = 3 Why does the latter work whereas the former does not? ...