tuples

How can I unpack a tuple struct like I would a classic tuple?

谁说胖子不能爱 提交于 2019-12-05 01:06:20
I can unpack a classic tuple like this: let pair = (1, true); let (one, two) = pair; If I have a tuple struct such as struct Matrix(f32, f32, f32, f32) and I try to unpack it, I get an error about "unexpected type": struct Matrix(f32, f32, f32, f32); let mat = Matrix(1.1, 1.2, 2.1, 2.2); let (one, two, three, four) = mat; Results in this error: error[E0308]: mismatched types --> src/main.rs:47:9 | 47 | let (one, two, three, four) = mat; | = note: expected type `Matrix` found type `(_, _, _, _)` How can I unpack a tuple struct? Do I need to convert it explicitly to a tuple type? Or do I need to

Is `namedtuple` really as efficient in memory usage as tuples? My test says NO

拥有回忆 提交于 2019-12-05 01:03:16
It is stated in the Python documentation that one of the advantages of namedtuple is that it is as memory-efficient as tuples. To validate this, I used iPython with ipython_memory_usage . The test is shown in the images below: The test shows that: 10000000 instances of namedtuple used about 850 MiB of RAM 10000000 tuple instances used around 73 MiB of RAM 10000000 dict instances used around 570 MiB of RAM So namedtuple used much more memory than tuple ! Even more than dict !! What do you think? Where did I go wrong? Billy A simpler metric is to check the size of equivalent tuple and namedtuple

Python, Convert 9 tuple UTC date to MySQL datetime format

て烟熏妆下的殇ゞ 提交于 2019-12-05 00:40:30
I am parsing RSS feeds with the format as specified here: http://www.feedparser.org/docs/date-parsing.html date tuple (2009, 3, 23, 13, 6, 34, 0, 82, 0) I am a bit stumped at how to get this into the MySQL datetime format (Y-m-d H:M:S)? tup = (2009, 3, 23, 13, 6, 34, 0, 82, 0) import datetime d = datetime.datetime(*(tup[0:6])) #two equivalent ways to format it: dStr = d.isoformat(' ') #or dStr = d.strftime('%Y-%m-%d %H:%M:%S') 来源: https://stackoverflow.com/questions/686717/python-convert-9-tuple-utc-date-to-mysql-datetime-format

Scala underscore explanation

戏子无情 提交于 2019-12-05 00:10:08
问题 Have a look at these scala snippets: if we have something like this: List(List(1, 2), List(3, 4), List(5)) map (x => (x.size)) we can shorten it to: List(List(1, 2), List(3, 4), List(5)) map ((_.size)) but, if we have something like this: List(List(1, 2), List(3, 4), List(5)) map (x => (x.size, x.size)) why can't we shorten it to: List(List(1, 2), List(3, 4), List(5)) map ((_.size, _.size)) ? 回答1: An amount of placeholders should be equals amount of function parameters. In your case map has 1

Swift tuple to Optional assignment

一笑奈何 提交于 2019-12-04 23:58:24
I am writing some code in Swift to learn the language. Here is my base class: import Foundation class BaseCommand:NSOperation { var status:Int? = nil var message:String? = nil func buildRequest() -> NSData? { return nil } func parseResponse(data:NSData?) -> (Status:Int, Error:String) { return (200, "Success") } override func main() { let requestBody = self.buildRequest() println("Sending body \(requestBody)") // do network op var networkResultBody = "test" var resultBody:NSData = networkResultBody.dataUsingEncoding(NSUTF8StringEncoding)! (self.status, self.message) = self.parseResponse

List lookup faster than tuple?

蓝咒 提交于 2019-12-04 23:56:25
In the past, when I've needed array-like indexical lookups in a tight loop, I usually use tuples, since they seem to be generally extremely performant (close to using just n-number of variables). However, I decided to question that assumption today and came up with some surprising results: In [102]: l = range(1000) In [103]: t = tuple(range(1000)) In [107]: timeit(lambda : l[500], number = 10000000) Out[107]: 2.465047836303711 In [108]: timeit(lambda : t[500], number = 10000000) Out[108]: 2.8896381855010986 Tuple lookups appear to take 17% longer than list lookups! Repeated experimentation

Can I extend Tuples in Swift?

◇◆丶佛笑我妖孽 提交于 2019-12-04 23:35:05
I'd like to write an extension for tuples of (e.g.) two value in Swift. For instance, I'd like to write this swap method: let t = (1, "one") let s = t.swap such that s would be of type (String, Int) with value ("one", 1) . (I know I can very easily implement a swap(t) function instead, but that's not what I'm interested in.) Can I do this? I cannot seem to write the proper type name in the extension declaration. Additionally, and I suppose the answer is the same, can I make a 2-tuple adopt a given protocol? You cannot extend tuple types in Swift. According to Types , there are named types

Converting lists of tuples to strings Python

笑着哭i 提交于 2019-12-04 23:28:49
I've written a function in python that returns a list, for example [(1,1),(2,2),(3,3)] But i want the output as a string so i can replace the comma with another char so the output would be '1@1' '2@2' '3@3' Any easy way around this?:) Thanks for any tips in advance This looks like a list of tuple s, where each tuple has two elements. ' '.join(['%d@%d' % (t[0],t[1]) for t in l]) Which can of course be simplified to: ' '.join(['%d@%d' % t for t in l]) Or even: ' '.join(map(lambda t: '%d@%d' % t, l)) Where l is your original list . This generates 'number@number' pairs for each tuple in the list.

Swift: Get an array of element from an array of tuples

守給你的承諾、 提交于 2019-12-04 22:34:54
I have an array of tuples like this : var answers: [(number: Int, good: Bool)] I want to get from it an array of number member. Like if I did something like : answers["number"] // -> Should give [Int] of all values named "number" I didn't find anything like it, maybe it's not possible, but it would be sad :( That's simple: answers.map { $0.number } var ints = answers.map { tuple in tuple.0 } If your tuple is not named you can do this: let mappedInts = answers.map({$0.0}) let mappedBools = answers.map({$0.1}) 来源: https://stackoverflow.com/questions/26536538/swift-get-an-array-of-element-from-an

Convert anonymous type to new C# 7 tuple type

放肆的年华 提交于 2019-12-04 22:17:01
The new version of C# is there, with the useful new feature Tuple Types: public IQueryable<T> Query<T>(); public (int id, string name) GetSomeInfo() { var obj = Query<SomeType>() .Select(o => new { id = o.Id, name = o.Name, }) .First(); return (id: obj.id, name: obj.name); } Is there a way to convert my anonymous type object obj to the tuple that I want to return without mapping property by property (assuming that the names of the properties match)? The context is in a ORM, my SomeType object has a lot of other properties and it is mapped to a table with lot of columns. I wanna do a query that