idioms

Is “regex” in modern programming languages really “context sensitive grammar”?

怎甘沉沦 提交于 2019-11-29 06:45:33
Over the years, "regex" pattern matching has been getting more and more powerful to the point where I wonder: is it really just context-sensitive-grammar matching? Is it a variation/extension of context-free-grammar matching? Where is it right now and why don't we just call it that instead of the old, restrictive "regular expression"? In particular backreferences to capturing parentheses make regular expressions more complex than regular, context-free, or context-sensitive grammars. The name is simply historically grown (as many words). See also this section in Wikipedia and this explanation

What is the template<typename T, T t> idiom?

蹲街弑〆低调 提交于 2019-11-29 06:31:52
问题 I was reading this and was trying to understand what N3601 was about. It said this idiom comes up a lot in a web search, but I couldn't find anything. What is the template<typename T, T t> idiom, what does it solve, how is it used, what is are Implicit template parameters, and what does the proposal aim to fix? 回答1: The problem that is being solved is deducing types from template non-type parameters. Given: template<typename T> void foo(T); template<typename T, T> void bar(); it is possible

idioms for returning multiple values in shell scripting

北战南征 提交于 2019-11-29 05:28:48
问题 Are there any idioms for returning multiple values from a bash function within a script? http://tldp.org/LDP/abs/html/assortedtips.html describes how to echo multiple values and process the results (e.g., example 35-17), but that gets tricky if some of the returned values are strings with spaces in. A more structured way to return would be to assign to global variables, like foo () { FOO_RV1="bob" FOO_RV2="bill" } foo echo "foo returned ${FOO_RV1} and ${FOO_RV2}" I realize that if I need re

Python indentation in “empty lines”

谁说我不能喝 提交于 2019-11-29 04:34:57
问题 Which is preferred ("." indicating whitespace)? A) def foo(): x = 1 y = 2 .... if True: bar() B) def foo(): x = 1 y = 2 if True: bar() My intuition would be B (that's also what vim does for me), but I see people using A) all the time. Is it just because most of the editors out there are broken? 回答1: The PEP 8 does not seem to be clear on this issue, although the statements about "blank lines" could be interpreted in favor of B. The PEP 8 style-checker (pep8.py) prefers B and warns if you use

Reason for Assignment to “ _ ” [duplicate]

浪尽此生 提交于 2019-11-28 23:23:56
This question already has an answer here: Underscore _ as variable name in Python [duplicate] 3 answers 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 question relates to the use of "_" in scripts, modules, etc., but not its use at the interactive prompt. In IDLE , the

Is the Perl Goatse 'Secret Operator' efficient?

两盒软妹~` 提交于 2019-11-28 22:50:36
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 things. the =()= causes an assignment to an empty list, so all the actual matches are copied to an empty

when to use if vs elif in python

扶醉桌前 提交于 2019-11-28 19:36:55
If I have a function with multiple conditional statements where every branch gets executed returns from the function. Should I use multiple if statements, or if/elif/else? For example, say I have a function: def example(x): if x > 0: return 'positive' if x < 0: return 'negative' return 'zero' Is it better to write: def example(x): if x > 0: return 'positive' elif x < 0: return 'negative' else: return 'zero' Both have the same outcome, but is one more efficient or considered more idiomatic than the other? Edit: A couple of people have said that in the first example both if statements are always

Union of dict objects in Python [duplicate]

∥☆過路亽.° 提交于 2019-11-28 18:30:19
This question already has an answer here: How to merge two dictionaries in a single expression? 41 answers How do you calculate the union of two dict objects in Python, where a (key, value) pair is present in the result iff key is in either dict (unless there are duplicates)? For example, the union of {'a' : 0, 'b' : 1} and {'c' : 2} is {'a' : 0, 'b' : 1, 'c' : 2} . Preferably you can do this without modifying either input dict . Example of where this is useful: Get a dict of all variables currently in scope and their values Mechanical snail This question provides an idiom. You use one of the

Best ruby idiom for “nil or zero”

狂风中的少年 提交于 2019-11-28 17:08:36
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. Christian Lescuyer 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 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. Ruby can make you search for a pretty way to do everything, if you're not careful. First

Hashes of Hashes Idiom in Ruby?

流过昼夜 提交于 2019-11-28 16:32:03
Creating hashes of hashes in Ruby allows for convenient two (or more) dimensional lookups. However, when inserting one must always check if the first index already exists in the hash. For example: h = Hash.new h['x'] = Hash.new if not h.key?('x') h['x']['y'] = value_to_insert It would be preferable to do the following where the new Hash is created automatically: h = Hash.new h['x']['y'] = value_to_insert Similarly, when looking up a value where the first index doesn't already exist, it would be preferable if nil is returned rather than receiving an undefined method for '[]' error. looked_up