tuples

Scala's tuple unwrapping nuance

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 21:59:28
I've noticed the following behavior in scala when trying to unwrap tuples into vals: scala> val (A, B, C) = (1, 2, 3) <console>:5: error: not found: value A val (A, B, C) = (1, 2, 3) ^ <console>:5: error: not found: value B val (A, B, C) = (1, 2, 3) ^ <console>:5: error: not found: value C val (A, B, C) = (1, 2, 3) ^ scala> val (u, v, w) = (1, 2, 3) u: Int = 1 v: Int = 2 w: Int = 3 Is that because scala's pattern matching mechanism automatically presumes that all identifiers starting with capitals within patterns are constants, or is that due to some other reason? Thanks! Yes, and it gets

List of Tuples to DataFrame Conversion [duplicate]

廉价感情. 提交于 2019-12-02 20:22:46
This question already has answers here : Construct pandas DataFrame from list of tuples of (row,col,values) (3 answers) I have a list of tuples similar to the below: [(date1, ticker1, value1),(date1, ticker1, value2),(date1, ticker1, value3)] I want to convert this to a DataFrame with index=date1 , columns=ticker1 , and values = values . What is the best way to do this? EDIT: My end goal is to create a DataFrame with a datetimeindex equal to date1 with values in a column labeled 'ticker': df = pd.DataFrame(tuples, index=date1) Right now the tuple is generated with the following: tuples=list

In Scala, is there a way to take convert two lists into a Map?

耗尽温柔 提交于 2019-12-02 20:19:35
I have a two lists, a List[A] and a List[B] . What I want is a Map[A,B] but I want the semantics of zip . So started out like so: var tuplesOfAB = listOfA zip listOfB Now I'm not sure how to construct a Map from my tuplesOfAB . As a follow-up question, I also want to invert my map so that from a Map[A,B] I can create a Map[B,A] . Can anyone hit me with a clue-stick? oxbow_lakes In 2.8 this is really simple using the CanBuildFrom functionality ( as described by Daniel ) and using breakOut with a type instruction to the compiler as to what the result type should be: import scala.collection

Tuple.Create() vs new Tuple

£可爱£侵袭症+ 提交于 2019-12-02 20:04:21
Consider the following expressions: new Tuple<int,int>(1,2); Tuple.Create(1,2); Is there any difference between these two methods of Tuple creation? From my reading it seems to be more a convenient shorthand than anything like object creation in C++ (heap vs stack). taquion Well, this questions is old... but nevertheless I think I may contribute constructively. From the accepted answer: I suppose one benefit is that, since you don't have to specify the type with Tuple.Create, you can store anonymous types for which you otherwise wouldn't be able to say what the type is The consequence is true:

Haskell tuple constructor (GHC) and the separation between a language and its implementation

我与影子孤独终老i 提交于 2019-12-02 19:58:54
Haskell blew my mind yet again when I realised that (x,y) Is just syntactic sugar for (,) x y Naturally I wanted to extend this to larger tuples. But (,) x ((,) y z) Gave me (x,(y,z)) Which was not what I was looking for. On a whim, I tried (,,) x y z And it worked, giving exactly what I wanted: (x,y,z) This raised the question: How far can you take it? Much to my astonishment, there seemed to be no limit. All of the below are valid operators: (,) (,,) (,,,) (,,,,) --etc (,,,,,,,,,,,,,,) (,,,,,,,,,,,,,,,) --etc (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,) --etc This behaviour is amazing and leads to

Add another tuple to a tuple of tuples

淺唱寂寞╮ 提交于 2019-12-02 18:42:27
I have the following tuple of tuples: my_choices=( ('1','first choice'), ('2','second choice'), ('3','third choice') ) and I want to add another tuple to the start of it another_choice = ('0', 'zero choice') How can I do this? the result would be: final_choices=( ('0', 'zero choice') ('1','first choice'), ('2','second choice'), ('3','third choice') ) Build another tuple-of-tuples out of another_choice , then concatenate: final_choices = (another_choice,) + my_choices Alternately, consider making my_choices a list-of-tuples instead of a tuple-of-tuples by using square brackets instead of

Building an unordered map with tuples as keys

陌路散爱 提交于 2019-12-02 18:09:28
In a C++ program with Boost, I am trying to build an unordered map whose keys are tuples of doubles: typedef boost::tuples::tuple<double, double, double, double> Edge; typedef boost::unordered_map< Edge, int > EdgeMap; Initializing the map completes OK, however, when I try to populate it with keys and values EdgeMap map; Edge key (0.0, 0.1, 1.1, 1.1); map[key] = 1; I encounter the following error message: /usr/include/boost/functional/hash/extensions.hpp:176: error: no matching function for call to ‘hash_value(const boost::tuples::tuple<double, double, double, double, boost::tuples::null_type,

How is std::tuple implemented?

孤者浪人 提交于 2019-12-02 18:03:14
I'd like to know how are tuple implemented in standard library for C++0x. I tried to read description in libstdc++ manual and then read template listing , but it's really hard to understand how it works, especially when reading code. Can someone explain me in few sentences the idea of tuple implementation? I want to know this, because I thinking about using tuples in my code and i want to understand how it works and what type of overhead does it brings (extends compile time only, perform many copy operations on memory, execute many other function in constructor, etc.). One approach to

How to pass a tuple argument the best way?

左心房为你撑大大i 提交于 2019-12-02 17:55:40
How to pass a tuple argument the best way ? Example: def foo(...): (Int, Int) = ... def bar(a: Int, b: Int) = ... Now I would like to pass the output of foo to bar . This can be achieved with: val fooResult = foo(...) bar(fooResult._1, fooResult._2) This approach looks a bit ugly, especially when we deal with a n -tuple with n > 2 . Also we have to store the result of foo in an extra value, because otherwise foo has to be executed more than once using bar(foo._1, foo._2) . Is there a better way to pass through the tuple as argument ? Tomasz Nurkiewicz There is a special tupled method available

How do I add x tuples into a list x number of times?

ⅰ亾dé卋堺 提交于 2019-12-02 17:45:44
问题 I have a question about tuples and lists in Haskell. I know how to add input into a tuple a specific number of times. Now I want to add tuples into a list an unknown number of times; it's up to the user to decide how many tuples they want to add. How do I add tuples into a list x number of times when I don't know X beforehand? 回答1: When doing functional programming, it is often better to think about composition of operations instead of individual steps. So instead of thinking about it like