idioms

Idiomatic use of ReentrantLock in Concurrency package [duplicate]

对着背影说爱祢 提交于 2019-12-05 04:34:59
问题 This question already has answers here : In ArrayBlockingQueue, why copy final member field into local final variable? (2 answers) Closed 3 years ago . While browsing through the source code of the java.util.concurrency package, I noticed an idiomatic use of ReentrantLock that I had not seen before: member RentrantLock variables were never accessed directly from within a method - they were always referenced by a local variable reference. Style #1 e.g from java.util.concurrent

ruby default argument idiom

我的未来我决定 提交于 2019-12-05 02:00:40
What's the idiom in Ruby when you want to have a default argument to a function, but one that is dependent on another parameter / another variable? For example, in Python, an example is: def insort_right(a, x, lo=0, hi=None): if hi is None: hi = len(a) while lo < hi: mid = (lo+hi)//2 if x < a[mid]: hi = mid else: lo = mid+1 a.insert(lo, x) Here, if hi is not supplied, it should be len(a) . You can't do len(a) in the default argument list, so you assign it a sentinel value, None, and check for that. What would the equivalent be in Ruby? def foo(a, l = a.size) end 来源: https://stackoverflow.com

Idiomatic Python logging: format string + args list vs. inline string formatting - which is preferred?

折月煮酒 提交于 2019-12-05 01:26:54
Is it advantageous to call logging functions with format string + args list vs. formatting inline? I've seen (and written) logging code that uses inline string formatting: logging.warn("%s %s %s" % (arg1, arg2, arg3)) and yet I assume it's better (performance-wise, and more idiomatic) to use: logging.warn("%s %s %s", arg1, arg2, arg3) because the second form avoids string formatting operations prior to invoking the logging function. If the current logging level would filter out the log message, no formatting is necessary, reducing computing time and memory allocations. Am I on the right track

Sink arguments and move semantics for functions that can fail (strong exception safety)

為{幸葍}努か 提交于 2019-12-05 01:08:52
I have a function that operates on a big chunk of data passed in as a sink argument. My BigData type is already C++11-aware and comes with fully functional move constructor and move assignment implementations, so I can get away without having to copy the damn thing: Result processBigData(BigData); [...] BigData b = retrieveData(); Result r = processBigData(std::move(b)); This all works perfectly fine. However, my processing function may fail occasionally at runtime resulting in an exception. This is not really a problem, since I can just fix stuff and retry: BigData b = retrieveData(); Result

Why implicitly check for emptiness in Python? [closed]

拥有回忆 提交于 2019-12-04 23:40:26
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 8 years ago . The Zen of Python says that explicit is better than implicit. Yet the Pythonic way of checking a collection c for emptiness is: if not c: # ... and checking if a collection is not empty is done like: if c: # ... ditto for anything that

Why isn't main defined `main(std::vector<std::string> args)`?

核能气质少年 提交于 2019-12-04 23:39:36
This question is only half tongue-in-cheek. I sometimes dream of a world without naked arrays or c strings. If you're using c++, shouldn't the preferred definition of main be something like: int main(std::vector<std::string> args) ? There are already multiple definitions of main to choose from, why isn't there a version that is in the spirit of C++? A concern that keeps coming back to my mind is that once you allow complex types, you end up with the risk of exceptions being thrown in the type's constructor. And, as the language is currently designed, there's absolutely no way for such an

substrings and the Go garbage collector

陌路散爱 提交于 2019-12-04 22:28:50
问题 When taking a substring of a string in Go, no new memory is allocated. Instead, the underlying representation of the substring contains a Data pointer that is an offset of the original string's Data pointer. This means that if I have a large string and wish to keep track of a small substring, the garbage collector will be unable to free any of the large string until I release all references to the shorter substring. Slices have a similar problem, but you can get around it by making a copy of

Why is there no boost::copy_on_write_ptr?

末鹿安然 提交于 2019-12-04 22:20:50
问题 I just saw this nice copy-on-write pointer implementation. It looks pretty generic and useful, so my question is: Is such a class contained in any of the C++ toolkits (boost, loki, etc.)? If not, I'd really like to know why because it is a really useful idiom and apparently a generic implementation seems doable (like the one I linked to). 回答1: There was a lot of debate over the possibility, and at least one suggested version of what eventually came out as auto_ptr was for a reference counted

In ruby, is truthiness idiomatic for a method name ending with a question mark?

可紊 提交于 2019-12-04 17:41:55
问题 Is it normal for methods with a question mark to return something that's truthy (for example, a number) to indicate that something is true, or should true itself be returned? Are there any examples of truthiness being used in the Ruby standard library or by Rails, for example? Background: Someone wrote a String#int? method in an answer to a separate question, which returned an integer to represent true, and nil to represent false. Another user was surprised at not returning a boolean. 回答1: It

What is the idiomatic way to slice an array relative to both of its ends?

一曲冷凌霜 提交于 2019-12-04 15:00:02
问题 Powershell's array notation has rather bizarre, albeit documented, behavior for slicing the end of arrays. This section from the official documentation sums up the bizarreness rather well: Negative numbers count from the end of the array. For example, "-1" refers to the last element of the array. To display the last three elements of the array, type: $a[-3..-1] However, be cautious when using this notation. $a[0..-2] This command does not refer to all the elements of the array, except for the