tuples

Alamofire Not Working Properly - Tuple Issue Different number of elements

為{幸葍}努か 提交于 2019-12-22 05:34:55
问题 Using Xcode 7.1 In the Alamofire responseJSON request I cannot put 4 parameters. Below is the code let url2 = "https://httpbin.org/get" Alamofire.request(.GET, url2).responseJSON{ request, response, JSON, error in print(JSON) } I get this error: Tuple types '(NSURLRequest?, NSHTTPURLResponse?, Result)' (aka '(Optional, Optional, Result)') and '(_, _, _, _)' have a different number of elements (3 vs. 4) If I remove the " error " parameter from responseJSON and run it...the app builds but no

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

六眼飞鱼酱① 提交于 2019-12-22 03:36:44
问题 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 `(_, _, _, _

Converting lists of tuples to strings Python

梦想与她 提交于 2019-12-22 03:30:16
问题 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 回答1: 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

Tuples and unpacking assignment support in C#?

隐身守侯 提交于 2019-12-22 02:06:34
问题 In Python I can write def myMethod(): #some work to find the row and col return (row, col) row, col = myMethod() mylist[row][col] # do work on this element But in C# I find myself writing out int[] MyMethod() { // some work to find row and col return new int[] { row, col } } int[] coords = MyMethod(); mylist[coords[0]][coords[1]] //do work on this element The Pythonic way is obivously much cleaner. Is there a way to do this in C#? 回答1: There's a set of Tuple classes in .NET: Tuple<int, int>

Are there tuples in PHP?

无人久伴 提交于 2019-12-22 01:49:43
问题 I know in Python and other languages we have access to tuples to better facilitate, semantically or otherwise, the structuring of data. My question is: Does PHP have tuples? If not, what is the nearest facility? 回答1: PHP's only real built in data structure that people use for everything is the array. Arrays in PHP are all hash tables and can have either numeric or string indexes and can contain anything (usually more arrays). There are a few array constructs that work like tuples. See http:/

Test assertions for tuples with floats

痞子三分冷 提交于 2019-12-21 13:08:40
问题 I have a function that returns a tuple that, among others, contains a float value. Usually I use assertAlmostEquals to compare those, but this does not work with tuples. Also, the tuple contains other data-types as well. Currently I am asserting every element of the tuple individually, but that gets too much for a list of such tuples. Is there any good way to write assertions for such cases? Consider this function: def f(a): return [(1.0/x, x * 2) for x in a] Now I want to write a test for it

Test assertions for tuples with floats

自古美人都是妖i 提交于 2019-12-21 13:07:45
问题 I have a function that returns a tuple that, among others, contains a float value. Usually I use assertAlmostEquals to compare those, but this does not work with tuples. Also, the tuple contains other data-types as well. Currently I am asserting every element of the tuple individually, but that gets too much for a list of such tuples. Is there any good way to write assertions for such cases? Consider this function: def f(a): return [(1.0/x, x * 2) for x in a] Now I want to write a test for it

General 'map' function for Scala tuples?

馋奶兔 提交于 2019-12-21 10:12:16
问题 I would like to map the elements of a Scala tuple (or triple, ...) using a single function returning type R. The result should be a tuple (or triple, ...) with elements of type R. OK, if the elements of the tuple are from the same type, the mapping is not a problem: scala> implicit def t2mapper[A](t: (A,A)) = new { def map[R](f: A => R) = (f(t._1),f(t._2)) } t2mapper: [A](t: (A, A))java.lang.Object{def map[R](f: (A) => R): (R, R)} scala> (1,2) map (_ + 1) res0: (Int, Int) = (2,3) But is it

Applying a function to each element of a tuple

匆匆过客 提交于 2019-12-21 09:56:03
问题 Given an std::tuple -like object (i.e. with defined tuple_size and get semantics) and a unary functor object ftor , I want to be able to call ftor on each element of the tuple -like object. If I disregard the return value, I am aware of the int array trick: namespace details { template <typename Ftor, typename Tuple, size_t... Is> void apply_unary(Ftor&& ftor, Tuple&& tuple, std::index_sequence<Is...>) { using std::get; int arr[] = { (ftor(get<Is>(std::forward<Tuple>(tuple))), void(), 0)... }

Applying a function to each element of a tuple

岁酱吖の 提交于 2019-12-21 09:55:44
问题 Given an std::tuple -like object (i.e. with defined tuple_size and get semantics) and a unary functor object ftor , I want to be able to call ftor on each element of the tuple -like object. If I disregard the return value, I am aware of the int array trick: namespace details { template <typename Ftor, typename Tuple, size_t... Is> void apply_unary(Ftor&& ftor, Tuple&& tuple, std::index_sequence<Is...>) { using std::get; int arr[] = { (ftor(get<Is>(std::forward<Tuple>(tuple))), void(), 0)... }