tuples

How to create a list of tuples C++

流过昼夜 提交于 2019-12-05 03:45:07
I am fairly new to c++, I dont really have any background in it. I am tying to create a list of tuples, the first will be an int, the second will be a string. #include <string> #include <list> #include <boost/tuple/tuple.hpp> .... list< tuple<int,string> > time; And getting an error. I want to be able to create a list, add entries that I can sort by the int, and have the string that describes, what the int was. How would I create this list? For a simple list use std::vector instead of std::list . You probably just want something simple such as: #include <iostream> #include <vector> #include

Return Tuple from Java Method

北战南征 提交于 2019-12-05 03:39:24
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? Generics aren't for primitive types. Use Boolean instead of boolean . public Tuple<Boolean, String> getResult() { //your code

How to create a non-TH package from code generated using Template Haskell?

若如初见. 提交于 2019-12-05 03:11:38
I'm making a small package that defines wrappers for tuples and adds instances form them, like newtype Tuple2 a = Tuple2 { untuple2 :: (a, a) } deriving (...) tuple2 :: a -> a -> Tuple2 a tuple2 = ... instance Traversable Tuple2 where ... instance Foldable Tuple2 where ... instance Functor Tuple2 where ... instance Applicative Tuple2 where ... This repeats from 2 to 15 , so it looks like a job for Template Haskell. The generated code is always Haskell 98 compatible, so I'd like the final result to be a Haskell 98 compatible package too. Is it possible to generate a piece of code using Template

Convert dict of nested lists to list of tuples

天涯浪子 提交于 2019-12-05 03:07:27
I have dict of nested list s: d = {'a': [[('a1', 1, 1), ('a2', 1, 2)]], 'b': [[('b1', 2, 1), ('b2', 2, 2)]]} print (d) {'b': [[('b1', 2, 1), ('b2', 2, 2)]], 'a': [[('a1', 1, 1), ('a2', 1, 2)]]} I need create list of tuple s like: [('b', 'b1', 2, 1), ('b', 'b2', 2, 2), ('a', 'a1', 1, 1), ('a', 'a2', 1, 2)] I tried: a = [[(k, *y) for y in v[0]] for k,v in d.items()] a = [item for sublist in a for item in sublist] I think my solution is a bit over-complicated. Is there some better, more pythonic, maybe one line solution? You were almost there: [(k, *t) for k, v in d.items() for t in v[0]] The v[0

Workarounds for generic variable in Swift

偶尔善良 提交于 2019-12-05 02:24:41
问题 So I have a typealias tuple public typealias MyTuple<T> = (key: T, value: String) In my ViewController, I want to declare an array of MyTuple with generic data type as I still don't know the type for key yet. However, from this it is impossible to have a generic-type variable in Swift. There are other workarounds as follows but I don't like either of them. Anyone has better ideas? class ViewController: UIViewController { var array1 = [MyTuple<T>]() // compile error of course var array2 =

Python 3: Removing an empty tuple from a list of tuples

我与影子孤独终老i 提交于 2019-12-05 02:24:38
问题 I have a list of tuples that reads as such: >>>myList [(), (), ('',), ('c', 'e'), ('ca', 'ea'), ('d',), ('do',), ('dog', 'ear', 'eat', 'cat', 'car'), ('dogs', 'cars', 'done', 'eats', 'cats', 'ears'), ('don',)] And I would like it to read as such: >>>myList [('',), ('c', 'e'), ('ca', 'ea'), ('d',), ('do',), ('dog', 'ear', 'eat', 'cat', 'car'), ('dogs', 'cars', 'done', 'eats', 'cats', 'ears'), ('don',)] i.e. I would like to remove the empty tuples () from the list. While doing this I want to

Limitations of PyTuple_SetItem

别说谁变了你拦得住时间么 提交于 2019-12-05 02:15:28
I have a Python extension module which creates a tuple as an attribute of another object, and sets items in the tuple. Whenever I execute this module in Python, I keep getting the error SystemError: bad argument to internal function After reading over the docs for PyTuple , and debugging my program for a few hours, I still couldn't figure out what the hell was going on. Running my program through a debugger indicated the problem was occurring within a library call inside the Python interpreter. So, finally, I looked at the Python source code, and at long last I realized the problem. The

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

我与影子孤独终老i 提交于 2019-12-05 01:35:11
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 (0 + ... + xs); }; assert(multi_apply(sum, t0, t1) == 1 + 2 + 3 + 4 + 5 + 6); I can think of various

Python list of tuples to list of int

落爺英雄遲暮 提交于 2019-12-05 01:22:42
So, I have x=[(12,), (1,), (3,)] (list of tuples) and I want x=[12, 1, 3] (list of integers) in best way possible? Can you please help? You didn't say what you mean by "best", but presumably you mean "most pythonic" or "most readable" or something like that. The list comprehension given by F3AR3DLEGEND is probably the simplest. Anyone who knows how to read a list comprehension will immediately know what it means. y = [i[0] for i in x] However, often you don't really need a list, just something that can be iterated over once. If you've got a billion elements in x , building a billion-element y

Function composition in Haskell with tuple arguments [duplicate]

南楼画角 提交于 2019-12-05 01:13:25
This question already has an answer here: Feed elements of a tuple to a function as arguments in Haskell? 3 answers 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. What you're looking to do is to take a function that operates on curried arguments, h , and apply it to the result of f , which is a tuple. This process, turning a function of two