tuples

Pass tuple's content as variadic function arguments

喜你入骨 提交于 2020-01-28 10:28:15
问题 I play with C++0x for some time and now I want to use variadic templates and tuple to implement class "Task". I'm going to pass Task objects into newly created threads (using pthread). Task class will contain function pointer to function which should be called inside thread and arguments for this function, simplified code: class TaskCaller { // ... virtual bool dispatch (void); }; template<typename ...T_arguments> Task : public TaskCaller { public: // ... Task (bool (*function) (T_arguments&.

Replacing ViewModels with Tuples

醉酒当歌 提交于 2020-01-25 08:31:05
问题 I've just started on a new project which currently contains a fair few ViewModel DTO classes. I'm wondering whether it would be useful and even good practice to replace many of these DTOs with Tuples and to use that as my method of transfering a bunch of objects to my views. What do you think? Is this a correct usage of the tuple or am I off the mark here? 回答1: Usually view models have metadata associated with them which among other allows you to perform validation and integrates with editor

Type tuple so that function chain has valid parameter and return types

柔情痞子 提交于 2020-01-25 06:39:36
问题 I'd like to create a type for this tuple / array below. Where this is valid: const funcs = [(a: string) => 1, (a: number) => 'A', (a: string) => 2] And this is invalid: const funcs = [(a: string) => 1, (a: number) => 2, (a: string) => 3] The difference being the return type for the middle function changed from string to number. Is it possible? type SubtractOne<T extends number> = [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

Using list comprehension

时光怂恿深爱的人放手 提交于 2020-01-24 12:42:48
问题 I have a string, dictionary in the form: ('(Laughter flower)', {'laughter': (8.5, 0.9313), 'flower': (7.88, 1.1718), 'the': (4.98, 0.9145), 'puppy': (7.58, 1.4581), 'died': (1.56, 1.198), 'laugh': (9.5, 0.1), 'flow': (2.3, 0.51) } ) Each parentheses is a tuple which corresponds to (score, standard deviation). I'm taking the average of just the first integer in each tuple. I've tried this: def score(string, d): if len(string) == 0: return 0 string = string.lower() included = [d[word][0]for

Using list comprehension

前提是你 提交于 2020-01-24 12:42:05
问题 I have a string, dictionary in the form: ('(Laughter flower)', {'laughter': (8.5, 0.9313), 'flower': (7.88, 1.1718), 'the': (4.98, 0.9145), 'puppy': (7.58, 1.4581), 'died': (1.56, 1.198), 'laugh': (9.5, 0.1), 'flow': (2.3, 0.51) } ) Each parentheses is a tuple which corresponds to (score, standard deviation). I'm taking the average of just the first integer in each tuple. I've tried this: def score(string, d): if len(string) == 0: return 0 string = string.lower() included = [d[word][0]for

Python how to convert a list of dict to a list of tuples

穿精又带淫゛_ 提交于 2020-01-23 08:16:40
问题 I have a list of dict that looks like this: list=[{u'hello':['001', 3], u'word':['003', 1], u'boy':['002', 2]}, {u'dad':['007', 3], u'mom':['005', 3], u'honey':['002', 2]} ] What I need is to iterate on my list in order to create list of tuples like this: new_list=[('hello','001', 3), ('word','003',1), ('boy','002', 2) ('dad','007',3), ('mom', '005', 3), ('honey','002',2)] NOTE! the numbers with the zeros ('001',003'... and so on) must be considerated as a string. Is there anybody whom can

Why does Tuple not have a Monad instance?

天涯浪子 提交于 2020-01-23 05:17:02
问题 One thing I noticed was that Tuple does not have a Monad instance. Tuple does however have an Applicative instance: instance Monoid a => Applicative ((,) a) Which already extremely heavily restricts what we can make the Monad instance be. Lets look at the type signature we would get for join: instance Monoid a => Monad ((,) a) join :: Monad m => m (m a) -> m a join :: Monoid a => (a, (a, b)) -> (a, b) We can also look at the Monad laws: join $ f <$> pure x == f x join $ f <$> (mempty, x) == f

How to create tuple with a loop in python

霸气de小男生 提交于 2020-01-23 04:54:09
问题 I want to create this tuple: a=(1,1,1),(2,2,2),(3,3,3),(4,4,4),(5,5,5),(6,6,6),(7,7,7),(8,8,8),(9,9,9) I tried with this a=1,1,1 for i in range (2,10): a=a,(i,i,i) However it creates a tuple inside other tuple in each iteration. Thank you 回答1: Use an extra comma in your tuples, and just join: a = ((1,1,1),) for i in range(2,10): a = a + ((i,i,i),) Edit : Adapting juanpa.arrivillaga's comment, if you want to stick with a loop, this is the right solution: a = [(1,1,1)] for i in range (2,10): a

Sorting a dict with tuples as values

十年热恋 提交于 2020-01-23 01:38:11
问题 I have a dictionary that looks like this: {'key_info': (rank, raw_data1, raw_data2), 'key_info2': ...} Basically I need back a list of the keys in sorted order, that is sorted based on the rank field in the tuple. My code looks something like this right now ( diffs is the name of the dict above): def _sortRanked(self): print(type(self.diffs)) return sorted(self.diffs.keys(), key=lambda x: x[1], reverse=True) that right now returns this when I run it: return sorted(self.diffs.keys(), key

Create a list of tuples with adjacent list elements if a condition is true

不想你离开。 提交于 2020-01-22 17:02:27
问题 I am trying to create a list of tuples where the tuple contents are the number 9 and the number before it in the list. Input List: myList = [1, 8, 9, 2, 4, 9, 6, 7, 9, 8] Desired Output: sets = [(8, 9), (4, 9), (7, 9)] Code: sets = [list(zip(myList[i:i], myList[-1:])) for i in myList if i==9] Current Result: [[], [], []] 回答1: Cleaner Pythonic approach: >>> [(x,y) for x,y in zip(myList, myList[1:]) if y == 9] [(8, 9), (4, 9), (7, 9)] What is the code above doing: zip(some_list, some_list[1:])