tuples

Return Tuple from Java Method

妖精的绣舞 提交于 2019-12-06 23:20:23
问题 I wrote a java class: public class Tuple<X, Y> { public final X x; public final Y y; public Tuple(X x, Y y) { this.x = x; this.y = y; } } but when I create a function like this: public Tuple<boolean, String> getResult() { try { if(something.equals(something2)) return new Tuple(true, null); } catch (Exception e){ return new Tuple(false, e.getMessage()); } However, I get the following compilation error: unexpected type required: reference found: boolean What I can do? 回答1: Generics aren't for

Applying multiple tuples to the same function (i.e. `apply(f, tuples…)`) without recursion or `tuple_cat`

白昼怎懂夜的黑 提交于 2019-12-06 22:21:21
问题 std::experimental::apply has the following signature: template <class F, class Tuple> constexpr decltype(auto) apply(F&& f, Tuple&& t); It basically invokes f by expanding t 's elements as the arguments. I would like something that does the exact same thing, but with multiple tuples at the same time: template <class F, class... Tuples> constexpr decltype(auto) multi_apply(F&& f, Tuples&&... ts); Example usage: std::tuple t0{1, 2, 3}; std::tuple t1{4, 5, 6}; auto sum = [](auto... xs){ return

Dictionary where keys are pair of integers in Python

让人想犯罪 __ 提交于 2019-12-06 21:56:57
问题 How is possible in Python to create a dictionary where the keys are pairs of integers? For example, if I do this: mydict=dict() mydict[ [1,2] ] = 'xxx' I get the error TypeError: unhashable type: 'list' So I came up with two different solutions: strings or tuples as keys. A first solution seems to convert the pair of integers in their string representation: mydict=dict() mydict[ str(1)+" "+str(2) ] = 'xxx' while the second solution involves tuples: mydict=dict() mydict[ tuple([1,2]) ] = 'xxx'

Iterate a list of tuples

╄→尐↘猪︶ㄣ 提交于 2019-12-06 20:29:10
问题 I'm looking for a clean way to iterate over a list of tuples where each is a pair like so [(a, b), (c,d) ...] . On top of that I would like to alter the tuples in the list. Standard practice is to avoid changing a list while also iterating through it, so what should I do? Here's what I kind of want: for i in range(len(tuple_list)): a, b = tuple_list[i] # update b's data # update tuple_list[i] to be (a, newB) 回答1: Just replace the tuples in the list; you can alter a list while looping over it,

Passing a tuple between two Panels in wxPython

对着背影说爱祢 提交于 2019-12-06 19:35:19
I am trying to pass the tuple of the the checked strings in one panel to the textCtrl widget in another panel. I thought that if I polled the panel class containing the checklistbox widget for the checked boxes and set that equal to an attribute of the panel class containing the TextCtrl widget I could do things in the TextCtrl widget based on the boxes that are checked I also tried using pubsub but I was having difficulty and wasn't sure if this was the case that you would use it or not. As a disclaimer I am new to object oriented programming and python. I am making progress in learning

Merge tuples with the same key

断了今生、忘了曾经 提交于 2019-12-06 18:57:43
问题 How to merge a tuple with the same key list_1 = [("AAA", [123]), ("AAA", [456]), ("AAW", [147]), ("AAW", [124])] and turn them into list_2 = [("AAA", [123, 456]), ("AAW", [147, 124])] 回答1: The most performant approach is to use a collections.defaultdict dictionary to store data as an expanding list, then convert back to tuple/list if needed: import collections list_1 = [("AAA", [123]), ("AAA", [456]), ("AAW", [147]), ("AAW", [124])] c = collections.defaultdict(list) for a,b in list_1: c[a]

Swift tuple to Optional assignment

老子叫甜甜 提交于 2019-12-06 18:28:13
问题 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 =

What's the best practice for handling single-value tuples in Python?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 18:23:47
问题 I am using a 3rd party library function which reads a set of keywords from a file, and is supposed to return a tuple of values. It does this correctly as long as there are at least two keywords. However, in the case where there is only one keyword, it returns a raw string, not a tuple of size one. This is particularly pernicious because when I try to do something like for keyword in library.get_keywords(): # Do something with keyword , in the case of the single keyword, the for iterates over

Function composition in Haskell with tuple arguments [duplicate]

我的未来我决定 提交于 2019-12-06 18:23:24
问题 This question already has answers here : Feed elements of a tuple to a function as arguments in Haskell? (3 answers) Closed 4 years ago . Sometimes I have two functions of the form: f :: a -> (b1,b2) h :: b1 -> b2 -> c and I need the composition g. I solve this by changing h to h': h' :: (b1,b2) -> c Can you please show me (if possible) a function m, so that: (h . m . f) == (h' . f) Or another way to deal with such situations. Thanks. 回答1: What you're looking to do is to take a function that

Can I extend Tuples in Swift?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 18:17:52
问题 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