idioms

An interesting C linked list idiom

回眸只為那壹抹淺笑 提交于 2019-11-27 18:25:34
I was at an interview for a C position in which they presented me with an idiom that I haven't previously encountered. This is a trick that simplifies implementation of various algorithms involving linked lists and I'm wondering if anybody else has encountered this. Say we have a linked list record defined so: typedef struct _record { char* value; struct _record* next; } record; We need a function that inserts a new record so that the entire list remains sorted with respect to the value's in the records. The following implementation is simpler than anything I would have used, albeit less

What is a programming idiom?

送分小仙女□ 提交于 2019-11-27 16:55:43
I see the phrase "programming idiom" thrown around as if it is commonly understood. Yet, in search results and stackoverflow I see everything... From micro: Incrementing a variable Representing an infinite loop Swapping variable values To medium: PIMPL RAII Format, comments, style... To macro: Programming paradigm or common library features as idiom Process model as idiom A collection of idioms equals a new paradigm Is there a single, common definition for "programming idiom"? Since "programming idiom" is used in many scopes: Micro: syntactic nuance or common syntax Medium: common style and

Reason for Assignment to “ _ ” [duplicate]

痞子三分冷 提交于 2019-11-27 14:44:52
问题 This question already has answers here : Underscore _ as variable name in Python [duplicate] (3 answers) Closed 5 years ago . I have seen this in a few contexts, e.g., in sequence unpacking: _, x = L.pop() # e.g., L is a list of tuples to initialize a container: X = _ So obviously this is not an element of the formal python syntax, rather the uses i am aware of appear discretionary. So I'm curious what is the likely reason for its use and what are the advantages generally (if any)? Note : my

Is the Perl Goatse 'Secret Operator' efficient?

孤街浪徒 提交于 2019-11-27 14:26:26
问题 The "goatse operator" or the =()= idiom in Perl causes an expression to be evaluated in list context. An example is: my $str = "5 and 4 and a 3 and 2 1 BLAST OFF!!!"; my $count =()= $str =~ /\d/g; # 5 matches... print "There are $count numbers in your countdown...\n\n"; As I interprete the use, this is what happens: $str =~ /\d/g matches all the digits. The g switch and list context produces a list of those matches. Let this be the "List Producer" example, and in Perl this could be many

How to name this key-oriented access-protection pattern?

回眸只為那壹抹淺笑 提交于 2019-11-27 14:21:14
Apparently this key-oriented access-protection pattern : class SomeKey { friend class Foo; SomeKey() {} // possibly non-copyable too }; class Bar { public: void protectedMethod(SomeKey); // only friends of SomeKey have access }; ... doesn't have a known name yet, thus i'd like to find a good one for it so we can refer to it without breaking our tongues. Suggestions? It should be: succinct convey the intent of access-protection ideally imply that no proxying is required (?) I like, in decreasing preference: passkey friend idiom passkey-door friend idiom pass-door friend idiom key-door friend

What is the idiomatic Hamcrest pattern to assert that each element of an iterable matches a given matcher?

戏子无情 提交于 2019-11-27 12:53:53
问题 Examine the following snippet: assertThat( Arrays.asList("1x", "2x", "3x", "4z"), not(hasItem(not(endsWith("x")))) ); This asserts that the list doesn't have an element that doesn't end with "x". This, of course, is the double negatives way of saying that all elements of the list ends with "x". Also note that the snippet throws: java.lang.AssertionError: Expected: not a collection containing not a string ending with "x" got: <[1x, 2x, 3x, 4z]> This lists the entire list, instead of just the

Idiom(s) for “for each except the last” (or “between each consecutive pair of elements”) [duplicate]

安稳与你 提交于 2019-11-27 11:52:07
This question already has an answer here: Printing lists with commas C++ 24 answers How can I check if I'm on the last element when iterating using foreach syntax [duplicate] 6 answers Everyone encounters this issue at some point: for(const auto& item : items) { cout << item << separator; } ... and you get an extra separator you don't want at the end. Sometime it's not printing, but, say, performing some other action, but such that consecutive actions of the same type require some separator action - but the last doesn't. Now, if you work with old-school for loops and an array, you would do for

Best ruby idiom for “nil or zero”

♀尐吖头ヾ 提交于 2019-11-27 10:04:18
问题 I am looking for a concise way to check a value to see if it is nil or zero. Currently I am doing something like: if (!val || val == 0) # Is nil or zero end But this seems very clumsy. 回答1: Objects have a nil? method. if val.nil? || val == 0 [do something] end Or, for just one instruction: [do something] if val.nil? || val == 0 回答2: If you really like method names with question marks at the end: if val.nil? || val.zero? # do stuff end Your solution is fine, as are a few of the other solutions

What C++ idioms should C++ programmers use? [closed]

Deadly 提交于 2019-11-27 09:01:35
问题 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 7 years ago . What C++ idioms should C++ programmers know? By C++ idioms, I mean design patterns or way of doing certain things that are only

Python idiom to return first item or None

冷暖自知 提交于 2019-11-27 09:01:00
问题 I'm sure there's a simpler way of doing this that's just not occurring to me. I'm calling a bunch of methods that return a list. The list may be empty. If the list is non-empty, I want to return the first item; otherwise, I want to return None. This code works: my_list = get_list() if len(my_list) > 0: return my_list[0] return None It seems to me that there should be a simple one-line idiom for doing this, but for the life of me I can't think of it. Is there? Edit: The reason that I'm looking