tuples

In Scala, how come `println(1,2)` works?

若如初见. 提交于 2019-12-01 15:54:15
In Scala (2.7.7final), the Predef.println method is defined as having the following signature: def println (x : Any) : Unit How come, then that the following works: scala> println(1,2) (1,2) Does the compiler automatically convert a comma-separated list of arguments into a Tuple? By what magic? Is there an implicit conversion going on here, and if so, which one? Yes, the compiler will attempt to convert comma separated arguments into tuples, if there are no appropriate multi-argument methods and a single appropriate one-argument method. It's not an implicit conversion, just a compiler hack.

Wrapping each type in a variadic template in a templated class

不羁的心 提交于 2019-12-01 15:49:10
Given a variadic template Types... , I would like to store A<> for each of the types in the pack. This could be done in a tuple of A<> 's, but I'd need to programmatically derive the type of said tuple. Is such a thing even possible in c++11/14/17? template <class T> class A { }; template <class... Types> class B { // A tuple of A<>'s for each type in Types... std::tuple<A<Type1>, A<Type2>, ...> data; }; Simply with: template <class... Types> class B { std::tuple<A<Types>...> data; }; 来源: https://stackoverflow.com/questions/32336822/wrapping-each-type-in-a-variadic-template-in-a-templated

How to Compare Multiple lists of tuples in python?

痞子三分冷 提交于 2019-12-01 15:31:45
How can I compare Multiple lists of tuples like this: [[(1,2), (3,6), (5,3)], [(1,5), (3,5)], [(2,1), (1,8), (3,9)]] The output should be: [(1,2), (1,5), (1,8)],[(3,6), (3,5), (3,9)] It means that i want just those values whose x-axis value matches others. (5,3) and (2,1) should be discarded! One possible Option >>> def group(seq): for k, v in groupby(sorted(chain(*seq), key = itemgetter(0)), itemgetter(0)): v = list(v) if len(v) > 1: yield v >>> list(group(some_list)) [[(1, 2), (1, 5), (1, 8)], [(3, 6), (3, 5), (3, 9)]] Another Popular Option >>> from collections import defaultdict >>> def

How do I pass tuples elements to a function as arguments in python?

左心房为你撑大大i 提交于 2019-12-01 15:18:06
I have a list consisting of tuples, I want to pass each tuple's elements to a function as arguments: mylist = [(a, b), (c, d), (e, f)] myfunc(a, b) myfunc(c, d) myfunc(e, f) How do I do it? Best Regards This is actually very simple to do in Python, simply loop over the list and use the splat operator ( * ) to unpack the tuple as arguments for the function: mylist = [(a, b), (c, d), (e, f)] for args in mylist: myfunc(*args) E.g: >>> numbers = [(1, 2), (3, 4), (5, 6)] >>> for args in numbers: ... print(*args) ... 1 2 3 4 5 6 To make @DSM's comment explicit: >>> from itertools import starmap >>>

Why is there no Tuple1 Literal for single element tuples in Scala?

雨燕双飞 提交于 2019-12-01 15:16:38
Python has (1,) for a single element tuple. In Scala, (1,2) works for Tuple2(1,2) but we must use Tuple1(1) to get a single element tuple. This may seem like a small issue but designing APIs that expect a Product is a pain to deal for users that are passing single elements since they have to write Tuple1(1). Maybe this is a small issue, but a major selling point of Scala is more typing with less typing. But in this case it seems it's more typing with more typing. Please tell me: 1) I've missed this and it exists in another form, or 2) It will be added to a future version of the language (and

N-ary tuples vs pairs

喜你入骨 提交于 2019-12-01 15:10:47
问题 In Ocaml, tuples with different arities have different type and value constructors: # let a = (1, 2, 3);; val a : int * int * int = (1, 2, 3) # let b = (1, (2, 3));; val b : int * (int * int) = (1, (2, 3)) Note that second example (b) is more flexible than first (a) because "tail" of b - (2, 3) - itself is valid value: # let (_, c) = b;; val c : int * int = (2, 3) # let d = snd b;; val d : int * int = (2, 3) What is the reason to not parse "(1, 2, 3)" as "(1, (2, 3))" and instead introduce

What's the difference between (1,) and (1) in python [duplicate]

三世轮回 提交于 2019-12-01 15:02:34
问题 This question already has answers here : How to create a tuple with only one element (4 answers) Closed 3 years ago . As stated in the title, I found that (1) and (1,) are different. But what's the difference of them? In[39]: (1) == (1,) Out[39]: False 回答1: The comma makes it a tuple. (1) is just the same as 1 wrapped in delimiters. 回答2: Try this to convince yourself: >>> type((1)) <type 'int'> >>> type((1,)) <type 'tuple'> The following identity checks may provide you with further insight

In Scala, how come `println(1,2)` works?

↘锁芯ラ 提交于 2019-12-01 14:58:43
问题 In Scala (2.7.7final), the Predef.println method is defined as having the following signature: def println (x : Any) : Unit How come, then that the following works: scala> println(1,2) (1,2) Does the compiler automatically convert a comma-separated list of arguments into a Tuple? By what magic? Is there an implicit conversion going on here, and if so, which one? 回答1: Yes, the compiler will attempt to convert comma separated arguments into tuples, if there are no appropriate multi-argument

Possible to initialize multiple variables from a tuple?

十年热恋 提交于 2019-12-01 14:39:59
问题 In some languages (such as PHP, Haskell, or Scala), you can assign multiple variables from tuples in a way that resembles the following pseudocode: list(string value1, string value2) = tupleWithTwoValues; I can't find a way to do this in C#, however, without writing longer, uglier code: string firstValue = tupleWithTwoValues.Item1; string secondValue = tupleWithTwoValues.Item2; This two-line solution is obviously not the end of the world, but I'm always looking for ways to write prettier code

how format specifier taking value while tuple list is passed

别来无恙 提交于 2019-12-01 14:37:37
I have a piece of code as below: tupvalue = [('html', 96), ('css', 115), ('map', 82)] So while printing the above tuple in the desired format for a particular index I found a code like this: >>> '%s:%d' % tupvalue[0] 'html:96' I'm wondering how the single value tupvalue[0] is recognised as a tuple of two values by the format specifier '%s:%d' ? Please explain this mechanism with a documentation reference. How can I use a comprehension to format all the values in tupvalue in the required format as in the example shown? First, the easy question: How can I use a comprehension to format all the