Why use tuples instead of objects?

First of all, a tuple is quick and easy: instead of writing a class for every time you want to put 2 things together, there’s a template that does it for you. Second of all, they’re generic. For example, in C++ the std::map uses an std::pair of key and value. Thus ANY pair can be … Read more

Are there any methods included in Scala to convert tuples to lists?

Works with any tuple (scala 2.8): myTuple.productIterator.toList Scala 2.7: (0 to (myTuple.productArity-1)).map(myTuple.productElement(_)).toList Not sure how to maintain type info for a general Product or Tuple, but for Tuple2: def tuple2ToList[T](t: (T,T)): List[T] = List(t._1, t._2) You could, of course, define similar type-safe conversions for all the Tuples (up to 22).

std::make_tuple doesn’t make references

std::tie makes non-const references. auto ref_tuple = std::tie(a,b); // decltype(ref_tuple) == std::tuple<int&, int&> For const references, you’ll either want the std::cref wrapper function: auto cref_tuple = std::make_tuple(std::cref(a), std::cref(b)); Or use a simply as_const helper to qualify the variables before passing them off to std::tie: template<class T> T const& as_const(T& v){ return v; } auto cref_tuple … Read more

Set (Collection) – Insert multiple elements

It was pointed out in the comments under the question, but I’d like to clearly state that there is a method for that very same purpose: mutating func formUnion<S>(_ other: S) where Element == S.Element, S : Sequence Usage: var attendees: Set = [“Alicia”, “Bethany”, “Diana”] let visitors = [“Diana”, “Marcia”, “Nathaniel”] attendees.formUnion(visitors) print(attendees) // … Read more

How do I sum the first value in each tuple in a list of tuples in Python?

In modern versions of Python I’d suggest what SilentGhost posted (repeating here for clarity): sum(i for i, j in list_of_pairs) In an earlier version of this answer I had suggested this, which was necessary because SilentGhost’s version didn’t work in the version of Python (2.3) that was current at the time: sum([pair[0] for pair in … Read more

Save list of ordered tuples as CSV [duplicate]

You can do this: import csv # note: If you use ‘b’ for the mode, you will get a TypeError # under Python3. You can just use ‘w’ for Python 3 data=[(‘smith, bob’,2),(‘carol’,3),(‘ted’,4),(‘alice’,5)] with open(‘ur file.csv’,’wb’) as out: csv_out=csv.writer(out) csv_out.writerow([‘name’,’num’]) for row in data: csv_out.writerow(row) # You can also do csv_out.writerows(data) instead of the for … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)