tuples

match tuple with null

和自甴很熟 提交于 2019-12-05 07:23:45
I don't understand why the following case doesn't match. Null should be an instance of Any, but it doesn't match. Can someone explain what is going on? val x = (2, null) x match { case (i:Int, v:Any) => println("got tuple %s: %s".format(i, v)) case _ => println("catch all") } prints catch all Thanks. This is exactly as specified. Type patterns consist of types, type variables, and wildcards. A type pattern T is of one of the following forms: * A reference to a class C, p.C, or T#C. This type pattern matches any non-null instance of the given class. It's interesting that so much relevance has

Time complexity of tuple in Python

萝らか妹 提交于 2019-12-05 07:10:01
There is similar question about hash (dictionaries) and lists, also there is a good piece of info here: http://wiki.python.org/moin/TimeComplexity But I didn't find anything about tuples. The access time for data_structure[i] for a linked list is in general O(n) for dictionary is ~ O(1) What about tuple? Is it O(n) like for a linked list or O(1) like for an array? It's O(1) for both list and tuple. They are both morally equivalent to an integer indexed array. Lists and tuples are indexable in the exact same way arrays are in other languages. A simplified explanation is that space is allocated

zip function help with tuples

你离开我真会死。 提交于 2019-12-05 06:55:36
I am hoping someone can help me with a problem I'm stuck with. I have a large number of tuples (>500) that look like this: (2,1,3,6) (1,2,5,5) (3,0,1,6) (10,1,1,4) (0,3,3,0) A snippet of my code reads: sum1 = (A,B,C,D) # creates a tuple of sums of (A,B,C,D) mysum = map(sum, zip(A, B, C, D)) print(mysum) I realize the above code is not correct. I am trying to find a way to add all the values A together, all the values of B together, all the values of C together, and all the values of D together using the zip function. For example, I would like to print something that looks like this: Asum = 16

the weird result_of<F(Ts…)> in Andrei Alexandrescu's talk about exploding tuple

好久不见. 提交于 2019-12-05 06:45:54
Has anyone watched Andrei Alexandrescu's talk about exploding tuple in GoingNative2013 yet? Here is the piece of code I don't quite follow: template <class F, class... Ts> auto explode(F&& f, const tuple<Ts...>& t) -> typename result_of<F(Ts...)>::type { return Expander<sizeof...(Ts), typename result_of<F(Ts...)>::type, F, const tuple<Ts...>&>::expand(f, t); } the F(Ts...) in result_of trouble me much. I mean: doesn't F stands for a function type ? I know R(Ts...) well, but the R here is a return type, but using F in place where R should be, that's the thing driving me crazy... Can anyone help

Weird behavior accessing tuple from Java

你。 提交于 2019-12-05 06:37:14
I am looking for explanation and/or versioning details (if possible) about a very strange behavior I found in Java accessing tuples created in Scala. I will show the weird behavior with an easy test I did. I created this Scala class: class Foo { def intsNullTuple = (null.asInstanceOf[Int], 2) def intAndStringNullTuple = (null.asInstanceOf[Int], "2") } and then I run this Java program: Tuple2<Object, Object> t = (new Foo()).intsNullTuple(); t._1(); // returns 0 ! t._1; // return null Tuple2<Object, String> t2 = (new Foo()).intAndStringNullTuple(); t._1(); // returns null t._1; // return null

F# parameter passing

谁说胖子不能爱 提交于 2019-12-05 06:02:28
I've always thought that F# had two different ways to pass arguments, curry style and tuple style. Is this actually correct? Isn't it simply one style , curry style, and arguments can either be simple values or tuples. e.g. someFunc (a,b) = isn't this a function with one curry style argument which happens to be a tuple ? Thus allowing me to pass tuples to this function using the pipleline operator? (where the elements of the tuple is named) (1,2) |> someFunc Is this correct? This will work just fine - the difference is when you have let f (a,b) = ... let f2 a b = ... then you can create a

Haskell Convert List to List of Tuples

空扰寡人 提交于 2019-12-05 05:49:31
i have a list like this ["peter","1000","michell","2000","kelly","3000"] and i would like to convert to [("peter",1000),("michell", 2000),("kelly",3000)] Please help. Thanks. cnv :: [String] -> [(String, Integer)] cnv [] = [] cnv (k:v:t) = (k, read v) : cnv t If you want to handle odd-length just add cnv [x] = variant before last one Travis Brown ony's solution is a bit shorter, but here's a non-recursive version using splitEvery from the very handy split library : cnv = map (\[name, amount] -> (name, read amount :: Int)) . splitEvery 2 The steps here are somewhat clearer (for me, at least)

Implicit cast function receiving tuple

拥有回忆 提交于 2019-12-05 05:27:15
问题 I just found out there's an implicit cast : From function taking n parameters (A, B, ...) -> R To function taking a n-tuple ((A, B, ...)) -> R Example n°1 func withTuple(_ a: (Int, Int)) { } func withoutTuple(_ a: Int, _ b: Int) { } func call(tupleFunction: ((Int, Int)) -> ()) { tupleFunction((1, 2)) } call(tupleFunction: withTuple) call(tupleFunction: withoutTuple) // Magic here (Valid Swift 4.2 code) Example n°2 [(1, 2), (3, 3)].map(*) // Magic here Is this behaviour documented somewhere?

Confusion while deriving from std::tuple, can not handle std::get

孤人 提交于 2019-12-05 04:08:04
My basic idea was to derive my own class from std::tuple to get some helper types inside like this: template <typename ... T> class TypeContainer: public std::tuple<T...> { public: using BaseType = std::tuple<T...>; static const size_t Size = sizeof...(T); TypeContainer(T... args):std::tuple<T...>(args...){}; using index_sequence = std::index_sequence_for<T...>; }; Now I try to use the code as follows: using MyType_tuple_with_empty = std::tuple< std::tuple<float,int>, std::tuple<>, std::tuple<int>>; using MyType_typecontainer_with_empty = TypeContainer< TypeContainer<float,int>, TypeContainer<

Do std::tuple and std::pair support aggregate initialization?

百般思念 提交于 2019-12-05 04:07:21
Aggregate initialization requires among other things no user-provided constructors . But std::tuple and std::pair pair have a large set of overloaded constructors . From the point of the core language, are these constructors user-provided or even user-declared ? With C++17 it will be possible to write (update/clarification: where nocopy is a class that can not be copied or moved, such as std::mutex ) auto get_ensured_rvo_str(){ return std::pair(std::string(),nocopy()); } edit: no, it's not possible as explained in the linked to answers and the answer below. which requires aggregate