tuples

Is it possible to infer template parameters of tuple from brace-type initialization?

不想你离开。 提交于 2019-11-30 15:40:52
In this example, is it possible to allow the deduction of the template parameters type of the tuple ? #include<tuple> #include<string> template<class T1, class T2> void fun(std::tuple<T1, T2> t, std::string other){} int main(){ fun(std::tuple<double, int>(2.,3), std::string("other")); // ok fun(std::make_tuple(2.,3), std::string("other")); // ok, but trying to avoid `make_tuple` fun({2.,3},std::string("other")); // desired syntax but // giving compilation error: candidate template ignored: couldn't infer template argument 'T1' void fun(std::tuple<T1, T2> t) } I added the second argument other

How to sum a list of tuples

给你一囗甜甜゛ 提交于 2019-11-30 12:13:00
Given the following list of tuples... val list = List((1, 2), (1, 2), (1, 2)) ... how do I sum all the values and obtain a single tuple like this? (3, 6) Using the foldLeft method. Please look at the scaladoc for more information. scala> val list = List((1, 2), (1, 2), (1, 2)) list: List[(Int, Int)] = List((1,2), (1,2), (1,2)) scala> list.foldLeft((0, 0)) { case ((accA, accB), (a, b)) => (accA + a, accB + b) } res0: (Int, Int) = (3,6) Using unzip . Not as efficient as the above solution. Perhaps more readable. scala> list.unzip match { case (l1, l2) => (l1.sum, l2.sum) } res1: (Int, Int) = (3

How to make a function that zips two tuples in C++11 (STL)?

狂风中的少年 提交于 2019-11-30 12:03:04
问题 I recently ran across this puzzle, was finally able to struggle out a hacky answer (using index arrays), and wanted to share it (answer below). I am sure there are answers that use template recursion and answers that use boost ; if you're interested, please share other ways to do this. I think having these all in one place may benefit others and be useful for learning some of the cool C++11 template metaprogramming tricks. Problem: Given two tuples of equal length: auto tup1 = std::make_tuple

Parsing a string which represents a list of tuples

落花浮王杯 提交于 2019-11-30 11:27:11
I have strings which look like this one: "(8, 12.25), (13, 15), (16.75, 18.5)" and I would like to convert each of them into a python data structure. Preferably a list (or tuple) of tuples containing a pair of float values. I could do that with eval("(8, 12.25), (13, 15), (16.75, 18.5)") which gives me a tuple of tuples, but I don't think naively evaluating external information would be a wise decision. So I wondered what an elegant pythonic solution might look like. >>> import ast >>> print ast.literal_eval("(8, 12.25), (13, 15), (16.75, 18.5)") ((8, 12.25), (13, 15), (16.75, 18.5)) def parse

When to use: Tuple vs Class c# 7.0

血红的双手。 提交于 2019-11-30 11:15:45
问题 Before Tuples, I used to create a class and its variables then create object from this class and make that object the return type for some functions. Now with the tuples I can do the same and in c# 7.0 we can assign understandable names for tuples properties (before this it was item1 , item2 , etc..) So now I am wondering, when should I use tuples and when should I create a class in c# 7.0? 回答1: As this answer is causing some confusion amongst some folk here, I should clarify that - as per

How do I reverse the order of element types in a tuple type?

丶灬走出姿态 提交于 2019-11-30 11:05:34
问题 How do I reverse the types in a tuple? For example, I want reverse_tuple<std::tuple<int, char, bool>>::type to be std::tuple<bool, char, int> . I tried doing the following but it didn't work. What did I do wrong? #include <type_traits> #include <tuple> template <typename... Ts> struct tuple_reverse; template <typename T, typename... Ts> struct tuple_reverse<std::tuple<T, Ts...>> { using type = typename tuple_reverse< std::tuple< typename tuple_reverse<std::tuple<Ts..., T>>::type > >::type; };

tuples vs records

久未见 提交于 2019-11-30 11:02:05
What is difference between tuples and records? Both are product types which let you build types from multiple simpler types. Some languages treat tuples as a kind of record. Definitions A tuple is an ordered group of elements, like (10, 25). A record is typically a group of named elements like { "x": 10, "y": 25 } where the value has two fields labelled x and y and the value of field x is 10 . Etymology The word "tuple" comes from the common "-tuple" suffix on "quintuple", "sextuple", "septuple", "octuple" which mean groups of 5, 6, 7, and 8 respectively. The word "record" comes from data

Python: Why is comparison between lists and tuples not supported?

橙三吉。 提交于 2019-11-30 11:01:10
When comparing a tuple with a list like ... >>> [1,2,3] == (1,2,3) False >>> [1,2,3].__eq__((1,2,3)) NotImplemented >>> (1,2,3).__eq__([1,2,3]) NotImplemented ... Python does not deep-compare them as done with (1,2,3) == (1,2,3) . So what is the reason for this? Is it because the mutable list can be changed at any time (thread-safety issues) or what? (I know where this is implemented in CPython, so please don't answer where , but why it is implemented.) You can always "cast" it >>> tuple([1, 2]) == (1, 2) True Keep in mind that Python, unlike for example Javascript, is strongly typed , and

Tuple pairs, finding minimum using python

戏子无情 提交于 2019-11-30 10:27:00
问题 I want to find the minimum of a list of tuples sorting by a given column. I have some data arranged as a list of 2-tuples for example. data = [ (1, 7.57), (2, 2.1), (3, 1.2), (4, 2.1), (5, 0.01), (6, 0.5), (7, 0.2), (8, 0.6)] How may I find the min of the dataset by the comparison of the second number in the tuples only? i.e. data[0][1] = 7.57 data[1][1] = 2.1 min( data ) = (5, 0.01) min( data ) returns (1, 7.57) , which I accept is correct for the minimum of index 0, but I want minimum of

How to sort an Array of Tuples?

前提是你 提交于 2019-11-30 10:25:00
How do you implement (or create) an array sort of a list of tuples? The following was gleaned from my code. Essentially I created an array of tuples and populated it via for loop; after which I tried to sort it. var myStringArray: (String,Int)[]? = nil ... myStringArray += (kind,number) ... myStringArray.sort{$0 > $1} This is what Xcode gave me before I could build: test.swift:57:9: '(String, Int)[]?' does not have a member named 'sort' drewag You have two problems. First, myStringArray is an Optional , you must "unwrap" it before you can call methods on it. Second, there is no > operator for