idioms

ruby syntactic sugar: dealing with nils

╄→гoц情女王★ 提交于 2019-12-04 13:47:40
问题 probably asked already but I couldn't find it.. here are 2 common situation (for me while programming rails..) that are frustrating to write in ruby: "a string".match(/abc(.+)abc/)[1] in this case I get an error because the string doesn't match, therefore the [] operator is called upon nil. What I'd like to find is a nicer alternative to the following: temp="a string".match(/abc(.+)abc/); temp.nil? ? nil : temp[1] in brief, if it didn't match simply return nil without the error The second

Preferred file line by line read idiom in Python

本小妞迷上赌 提交于 2019-12-04 05:56:46
I feel like almost every time I read a file in Python, what I want is: with open("filename") as file_handle: for line in file_handle: #do something Is this truly the preferred idiom? It mildly irritates me that this double indents all file reading logic. Is there a way to collapse this logic into one line or one layer? For simple cases, yes, the two-level with and for is idiomatic. For cases where the indentation becomes a problem, here as anywhere else in Python, the idiomatic solution is to find something to factor out into a function. You can write wrappers to help this. For example, here's

Python - Idiom to check if string is empty, print default

陌路散爱 提交于 2019-12-04 05:40:25
I'm just wondering, is there a Python idiom to check if a string is empty, and then print a default if it's is? (The context is Django, for the __unicode__(self) function for UserProfile - basically, I want to print the first name and last name, if it exists, and then the username if they don't both exist). Cheers, Victor displayname = firstname + lastname or username will work if firstname and last name has 0 length blank string displayname = firstname+' '+lastname if firstname and lastname else username I think this issue is better handled in the templates with something like: {{ user.get

Javascript regexp: replacing $1 with f($1)

别说谁变了你拦得住时间么 提交于 2019-12-04 03:49:10
问题 I have a regular expression, say /url.com\/([A-Za-z]+)\.html/ , and I would like to replace it with new string $1: f($1) , that is, with a constant string with two interpolations, the captured string and a function of the captured string. What's the best way to do this in JavaScript? 'Best' here means some combination of (1) least error-prone, (2) most efficient in terms of space and speed, and (3) most idiomatically appropriate for JavaScript, with a particular emphasis on #3. 回答1: The

Idiomatic use of ReentrantLock in Concurrency package [duplicate]

别说谁变了你拦得住时间么 提交于 2019-12-03 21:37:33
This question already has an answer here: In ArrayBlockingQueue, why copy final member field into local final variable? 2 answers 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.ThreadPoolExecutor private final ReentrantLock mainLock = new ReentrantLock(); ... // why a local reference to final mainlock instead of

Script to run against stdin if no arg; otherwise input file =ARGV[0]

天大地大妈咪最大 提交于 2019-12-03 19:34:31
问题 This works quite nicely - just wondered if there are any improvements to shorten it ? if (ARGV[0].nil?) then input=$< else input=File.new(ARGV[0],"r"); end ... # Do something with the input here, for example: input.each_line do |line| puts line end 回答1: You can eliminate the first five lines entirely. From Pickaxe $<: An object that provides access to the concatenation of the contents of all the files given as command-line arguments or $stdin (in the case where there are no arguments). $<

Elegant way to remove contiguous repeated elements in a list?

。_饼干妹妹 提交于 2019-12-03 16:47:14
I'm looking for a clean, Pythonic, way to eliminate from the following list: li = [0, 1, 2, 3, 3, 4, 3, 2, 2, 2, 1, 0, 0] all contiguous repeated elements (runs longer than one number) so as to obtain: re = [0, 1, 2, 4, 3, 1] but although I have working code, it feels un-Pythonic and I am quite sure there must be a way out there (maybe some lesser known itertools functions?) to achieve what I want in a far more concise and elegant way. Here is a version based on Karl's which doesn't requires copies of the list ( tmp , the slices, and the zipped list). izip is significantly faster than (Python

dplyr idiom for summarize() a filtered-group-by, and also replace any NAs due to missing rows

柔情痞子 提交于 2019-12-03 16:25:11
I am computing a dplyr::summarize across a dataframe of sales data. I do a group-by (S,D,Y), then within each group, compute medians and means for weeks 5..43, then merge those back into the parent df. Variable X is sales. X is never NA (i.e. there are no explicit NAs anywhere in df), but if there is no data (as in, no sales) for that S,D,Y and set of weeks, there will simply be no row with those values in df (take it that means zero sales for that particular set of parameters). In other words, impute X=0 in any structurally missing rows (but I hope I don't need to melt/cast the original df,

Why is there no boost::copy_on_write_ptr?

时光怂恿深爱的人放手 提交于 2019-12-03 14:37:54
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). 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 COW pointer. Unfortunately, the time for COW has mostly passed. Making a COW pointer (or COW-whatever)

substrings and the Go garbage collector

拜拜、爱过 提交于 2019-12-03 13:34:05
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 the subslice using copy(). I am unaware of any similar copy operation for strings. What is the