tuples

Applying a function to each element of a tuple

六眼飞鱼酱① 提交于 2019-12-21 09:55:39
问题 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)... }

Passing a tuple as command line argument [duplicate]

为君一笑 提交于 2019-12-21 07:55:14
问题 This question already has answers here : converting string to tuple (3 answers) Closed 4 years ago . My requirement is to pass a tuple as command line argument like --data (1,2,3,4) I tried to use the argparse module, but if I pass like this it is receiving as the string '(1,2,3,4)' . I tried by giving type=tuple for argparse.add_argument , but is of no use here. Do I have to add a new type class and pass that to type argument of add_argument ? Update I tried the ast.literal_eval based on

Passing a tuple as command line argument [duplicate]

天涯浪子 提交于 2019-12-21 07:51:20
问题 This question already has answers here : converting string to tuple (3 answers) Closed 4 years ago . My requirement is to pass a tuple as command line argument like --data (1,2,3,4) I tried to use the argparse module, but if I pass like this it is receiving as the string '(1,2,3,4)' . I tried by giving type=tuple for argparse.add_argument , but is of no use here. Do I have to add a new type class and pass that to type argument of add_argument ? Update I tried the ast.literal_eval based on

What is the difference between value constructors and tuples?

邮差的信 提交于 2019-12-21 07:29:30
问题 It's written that Haskell tuples are simply a different syntax for algebraic data types. Similarly, there are examples of how to redefine value constructors with tuples. For example, a Tree data type in Haskell might be written as data Tree a = EmptyTree | Node a (Tree a) (Tree a) which could be converted to "tuple form" like this: data Tree a = EmptyTree | Node (a, Tree a, Tree a) What is the difference between the Node value constructor in the first example, and the actual tuple in the

Pythonic shortcut for doubly nested for loops?

半城伤御伤魂 提交于 2019-12-21 07:22:03
问题 Consider if I had a function that took a tuple argument (x,y), where x was in the range(X), and y in the range(Y), the normal way of doing it would be: for x in range(X): for y in range(Y): function(x,y) is there a way to do for xy in something_like_range(X,Y): function(xy) such that xy was a tuple (x,y)? 回答1: You can use product from itertools >>> from itertools import product >>> >>> for x,y in product(range(3), range(4)): ... print (x,y) ... (0, 0) (0, 1) (0, 2) (0, 3) (1, 0) (1, 1) (1, 2)

How to unpack multiple tuples in function call

半城伤御伤魂 提交于 2019-12-21 07:09:28
问题 If I have a function def f(a, b, c, d) and two tuples, each with two elements, is there any way to unpack these tuples so that I can send their values to the function? f(*tup1, *tup2) 回答1: As of the release of Python 3.5.0, PEP 448 "Additional Unpacking Generalizations" makes the natural syntax for this valid Python: >>> f(*tup1, *tup2) 1 2 2 3 In older versions of Python, you can need to concatenate the tuples together to provide a single expanded argument: >>> tup1 = 1, 2 >>> tup2 = 2, 3 >>

“Tuple comprehensions” and the star splat/unpack operator *

混江龙づ霸主 提交于 2019-12-21 05:36:18
问题 I just read the question Why is there no tuple comprehension in Python? In the comments of the accepted answer, it is stated that there are no true "tuple comprehensions". Instead, our current option is to use a generator expression and pass the resulting generator object to the tuple constructor: tuple(thing for thing in things) Alternatively, we can create a list using a list comprehension and then pass the list to the tuple constructor: tuple([thing for thing in things]) Lastly and to the

Scala: List[Tuple3] to Map[String,String]

青春壹個敷衍的年華 提交于 2019-12-21 05:28:07
问题 I've got a query result of List[(Int,String,Double)] that I need to convert to a Map[String,String] (for display in an html select list) My hacked solution is: val prices = (dao.getPricing flatMap { case(id, label, fee) => Map(id.toString -> (label+" $"+fee)) }).toMap there must be a better way to achieve the same... 回答1: A little more concise: val prices = dao.getPricing.map { case (id, label, fee) => ( id.toString, label+" $"+fee)} toMap shorter alternative: val prices = dao.getPricing.map

Unpacking tuples in a python list comprehension (cannot use the *-operator)

筅森魡賤 提交于 2019-12-20 18:33:05
问题 I am trying to create a list based on another list, with the same values repeated 3 times consecutively. At the moment, I am using: >>> my_list = [ 1, 2 ] >>> three_times = [] >>> for i in range( len( my_list ) ): ... for j in range( 3 ): ... three_times.append( my_list[ i ] ) ... >>> print three_times [1, 1, 1, 2, 2, 2] But I would like to do it using a more Pythonic way, such as: >>> my_list = [ 1, 2 ] >>> three_times = [] >>> three_times = [ (value,) * 3 for value in my_list ] >>> print

Detect if a type is a std::tuple?

∥☆過路亽.° 提交于 2019-12-20 18:09:45
问题 Currently I have two functions : template<typename Type> bool f(Type* x); template<typename... List> bool f(std::tuple<List...>* x); Is there any way to merge these two functions with an extra template parameter that indicates whether the passed type is a tuple ? template<typename Type, bool IsTuple = /* SOMETHING */> bool f(Type* x); 回答1: Sure, using is_specialization_of (link taken and fixed from here): template<typename Type, bool IsTuple = is_specialization_of<Type, std::tuple>::value>