idioms

Is returning nil from a [[class alloc] init] considered good practice?

ⅰ亾dé卋堺 提交于 2019-12-24 02:11:50
问题 And is it a common idiom in Objective-C. I've only seen this used on [[NSImage alloc] initWithContentsOfFile: str] and it always make me think there is a memory leak, because i called alloc and the mantra is: "Call alloc and you must call release" - unless its one of the cases where you don't need to. 回答1: It is a common idiom to indicate a error in initializing the object. You are correct, however, the allocated instance must be released. So the pattern would be - (id)init { self = [super

How to use remove-erase idiom for removing empty vectors in a vector?

坚强是说给别人听的谎言 提交于 2019-12-24 00:43:00
问题 I have some trouble with removing a empty vector in a vector using the remove-erase idiom like Erasing elements from a vector. How can I apply this on: vector<vector<Point> > contours; // want to remove contours.at(i).empty() contours.erase(remove(contours.begin(), contours.end(), ??? ),contours.end()); 回答1: Have you tried: contours.erase(remove(contours.begin(), contours.end(), vector<Point>()), contours.end()); 回答2: Use remove_if that takes a predicate. contours.erase( std::remove_if(

Clojure message handling / async, multithreaded

六眼飞鱼酱① 提交于 2019-12-23 11:53:11
问题 I have a small Clojure consumer/publisher receiving messages, processing them and sending them off to other consumers, all via RabbitMQ. I've defined a message-handler that handles messages in a separate thread (separate from the main thread that is). As can be seen in the code below, the thread synchronously receives and sends messages, all happening in an event loop started by the lcm/subscribe function. So, the question is, what would be the "Clojure way" to create a N-sized thread pool of

What are the best ways to compare the contents of two list-like objects?

橙三吉。 提交于 2019-12-23 09:38:18
问题 When I have to compare the contents of two array-like objects -- for instance list s, tuple s or collection.deque s -- without regard for the type of the objects, I use list(an_arrayish) == list(another_arrayish) Is there any more idiomatic/faster/better way to achieve this? 回答1: Compare it elementwise: def compare(a,b): if len(a) != len(b): return False return all(i == j for i,j in itertools.izip(a,b)) For Python 3.x, use zip instead 回答2: Tuples appear to be faster: tuple(an_arrayish) ==

what is the work-around to “non-static variable cannot be referenced from a static context”? [closed]

纵然是瞬间 提交于 2019-12-23 07:06:30
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . There's a particular idiom to putting a method, or perhaps anonymous inner class, somehow, into the main method of a driver class: package net.bounceme.dur.misc; import net.bounceme.dur.misc.Foo; public class

Best practice for long string literals in Go

别来无恙 提交于 2019-12-23 06:48:23
问题 I've got a long string literal in Go: db.Exec("UPDATE mytable SET (I, Have, Lots, Of, Fields) = ('suchalongvalue', 'thisislongaswell', 'ohmansolong', 'wowsolong', 'loooooooooooooooooooooooooong')") I see two ways to make this more manageable: raw quotes, or multiple concatenated quotes: db.Exec(`UPDATE mytable SET (I, Have, Lots, Of, Fields) = ('suchalongvalue', 'thisislongaswell', 'ohmansolong', 'wowsolong', 'loooooooooooooooooooooooooong')`) db.Exec("UPDATE mytable SET (I, Have, Lots, Of,

x or y: acceptable idiom, or obfuscation?

女生的网名这么多〃 提交于 2019-12-22 08:59:10
问题 I have to extract values from a variable that may be None, with some defaults in mind. I first wrote this code: if self.maxTiles is None: maxX, maxY = 2, 2 else: maxX, maxY = self.maxTiles Then I realized I could shorten it to: maxX, maxY = self.maxTiles if self.maxTiles is not None else (2, 2) But then I realized this might be the most succinct and easily readable: maxX, maxY = self.maxTiles or (2, 2) Is the latter acceptable, or too hackish? 回答1: About, specifically, self.maxTiles if self

What is the “?” and “:” sequence actually called? [duplicate]

旧城冷巷雨未停 提交于 2019-12-22 08:15:58
问题 This question already has answers here : What does '?' do in C++? (7 answers) Closed 6 years ago . This may be a bonehead question, but I cannot figure out what the ? exp : other_exp sequence is called. Example: int result = (true) ? 1 : 0; I've tried using the Google machine, but it's hard to Googilize for something without knowing what it's called. Thanks! 回答1: It is called the the conditional operator or alternativly the ternary operator as it a ternary operator (an operator which takes 3

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

╄→尐↘猪︶ㄣ 提交于 2019-12-22 01:56:10
问题 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++? 回答1: 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

Why implicitly check for emptiness in Python? [closed]

僤鯓⒐⒋嵵緔 提交于 2019-12-22 01:12:40
问题 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