idioms

How should I loop over the elements of a C++ container in reverse order? [duplicate]

你。 提交于 2020-06-14 08:32:09
问题 This question already has answers here : Iterating C++ vector from the end to the begin (11 answers) Closed 5 hours ago . Suppose I'm a newbie C++ programmer. I have a C++ container; say, a vector: std::vector<int> vec { 12, 34, 56, 78 }; I know I can iterate over all of the elements with a simple loop: for(std::vector<int>::size_type i = 0; i < vec.size(); i++) { std::cout << vec[i] << '\n'; } and maybe I've even learned a little about Modern C++, so I know I can use a ranged-for loop: for

Ruby: Finding most recently modified file

偶尔善良 提交于 2020-01-30 17:47:40
问题 What's an idiomatic way to find the most recently modified file within a directory? 回答1: Dir.glob("*").max_by {|f| File.mtime(f)} 回答2: Dir["*"].sort { |a,b| File.mtime(a) <=> File.mtime(b) }.last This is not recursive. 回答3: I'm not sure if there really is an idiom for this. I would do Dir["*"].sort_by { |file_name| File.stat(file_name).mtime } Edit Seeing how three people gave more or less the same answer at the same time. This must be it. 来源: https://stackoverflow.com/questions/4823507/ruby

Idiom for simulating run-time numeric template parameters?

天涯浪子 提交于 2020-01-28 02:29:33
问题 Suppose we have template <unsigned N> foo() { /* ... */ } defined. Now, I want to implement do_foo(unsigned n); which calls the corresponding variant of foo() . This is not merely a synthetic example - this does actually happen in real life (of course, not necessarily with void-to-void functions and just one template parameter, but I'm simplfying. Of course, in C++, we can't have the following: do_foo(unsigned n) { foo<n>(); } and what I do right now is do_foo(unsigned n) { switch(n) { case n

Idiom for simulating run-time numeric template parameters?

安稳与你 提交于 2020-01-28 02:29:16
问题 Suppose we have template <unsigned N> foo() { /* ... */ } defined. Now, I want to implement do_foo(unsigned n); which calls the corresponding variant of foo() . This is not merely a synthetic example - this does actually happen in real life (of course, not necessarily with void-to-void functions and just one template parameter, but I'm simplfying. Of course, in C++, we can't have the following: do_foo(unsigned n) { foo<n>(); } and what I do right now is do_foo(unsigned n) { switch(n) { case n

Followup to returning nil from a [[class alloc] init]

主宰稳场 提交于 2020-01-16 04:42:26
问题 As follow-up of sorts to Is returning nil from a [[class alloc] init] considered good practice?, there's a case that I haven't seen any discussed much: what to do with an init that fails some preconditions before it can call the next init? Example, suppose in this initWithStuff: method being passed nil or in general having no value to pass to initWithValue: is an absolute failure and we definitely want to return nil. - (id)initWithStuff:(Stuff *)inStuff { if (!inStuff || ![inStuff

Better pattern for partial specialization disambiguation precedence chain?

拈花ヽ惹草 提交于 2020-01-13 11:39:28
问题 Consider the following series of partial specializations: template <typename T, typename Enable=void> struct foo { void operator()() const { cout << "unspecialized" << endl; } }; template <typename T> struct foo<T, enable_if_t< is_integral<T>::value >>{ void operator()() const { cout << "is_integral" << endl; } }; template <typename T> struct foo<T, enable_if_t< sizeof(T) == 4 and not is_integral<T>::value >>{ void operator()() const { cout << "size 4" << endl; } }; template <typename T>

Python negative zero slicing

萝らか妹 提交于 2020-01-13 09:34:13
问题 I often find myself having to work with the last n items in a sequence, where n may be 0. The problem is that trying to slice with [-n:] won't work in the case of n == 0 , so awkward special case code is required. For example if len(b): assert(isAssignableSeq(env, self.stack[-len(b):], b)) newstack = self.stack[:-len(b)] + a else: #special code required if len=0 since slice[-0:] doesn't do what we want newstack = self.stack + a My question is - is there any way to get this behavior without

What are the important language features (idioms) of Python to learn early on [duplicate]

拥有回忆 提交于 2020-01-11 14:48:32
问题 This question already has answers here : The Zen of Python [closed] (22 answers) Python: Am I missing something? [closed] (16 answers) Closed 5 years ago . I would be interested in knowing what the StackOverflow community thinks are the important language features (idioms) of Python. Features that would define a programmer as Pythonic. Python (pythonic) idiom - "code expression" that is natural or characteristic to the language Python. Plus, Which idioms should all Python programmers learn

Python string concatenation Idiom. Need Clarification.

北城以北 提交于 2020-01-11 06:06:32
问题 From http://jaynes.colorado.edu/PythonIdioms.html "Build strings as a list and use ''.join at the end. join is a string method called on the separator, not the list. Calling it from the empty string concatenates the pieces with no separator, which is a Python quirk and rather surprising at first. This is important: string building with + is quadratic time instead of linear! If you learn one idiom, learn this one. Wrong: for s in strings: result += s Right: result = ''.join(strings)" I'm not

Python string concatenation Idiom. Need Clarification.

主宰稳场 提交于 2020-01-11 06:06:31
问题 From http://jaynes.colorado.edu/PythonIdioms.html "Build strings as a list and use ''.join at the end. join is a string method called on the separator, not the list. Calling it from the empty string concatenates the pieces with no separator, which is a Python quirk and rather surprising at first. This is important: string building with + is quadratic time instead of linear! If you learn one idiom, learn this one. Wrong: for s in strings: result += s Right: result = ''.join(strings)" I'm not