idioms

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

荒凉一梦 提交于 2019-12-21 05:34:05
问题 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

Elegant way to remove contiguous repeated elements in a list?

不打扰是莪最后的温柔 提交于 2019-12-21 05:19:17
问题 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. 回答1: Here is a version based on Karl's which doesn't

Basic C++ Idioms / Techniques

一曲冷凌霜 提交于 2019-12-20 10:40:10
问题 Note: marked as community wiki. In recent days, I've realized how little I know about C++. Besides: using the STL implementing RAII implementing ref-counted smart pointers writing my own policy-based template classes overloading operators << for fun What other techniques are must-know for a good C++ programmer? Thanks! 回答1: I think this should cover it: More C++ Idioms - Wikibooks 回答2: OO Design Types of exception safety guarantees (which is what most design patterns/idioms are based on).

PHP - best way to initialize an object with a large number of parameters and default values

浪尽此生 提交于 2019-12-20 08:48:53
问题 I'm designing a class that defines a highly complex object with a ton (50+) of mostly optional parameters, many of which would have defaults (eg: $type = 'foo'; $width = '300'; $interactive = false; ). I'm trying to determine the best way to set up the constructor and instance/class variables in order to be able to: make it easy to use the class make it easy to auto-document the class (ie: using phpDocumentor) code this elegantly In light of the above, I don't want to be passing the

Get dplyr count of distinct in a readable way

匆匆过客 提交于 2019-12-20 08:27:28
问题 I'm new using dplyr, I need to calculate the distinct values in a group. Here's a table example: data=data.frame(aa=c(1,2,3,4,NA), bb=c('a', 'b', 'a', 'c', 'c')) I know I can do things like: by_bb<-group_by(data, bb, add = TRUE) summarise(by_bb, mean(aa, na.rm=TRUE), max(aa), sum(!is.na(aa)), length(aa)) But if I want the count of unique elements? I can do: > summarise(by_bb,length(unique(unlist(aa)))) bb length(unique(unlist(aa))) 1 a 2 2 b 1 3 c 2 and if I want to exclude NAs I cand do: >

Check whether a variable is a string in Ruby

南楼画角 提交于 2019-12-20 08:05:17
问题 Is there anything more idiomatic than the following? foo.class == String 回答1: I think you are looking for instance_of? . is_a? and kind_of? will return true for instances from derived classes. class X < String end foo = X.new foo.is_a? String # true foo.kind_of? String # true foo.instance_of? String # false foo.instance_of? X # true 回答2: A more duck-typing approach would be to say foo.respond_to?(:to_str) to_str indicates that an object's class may not be an actual descendant of the String,

What are some C++ related idioms, misconceptions, and gotchas that you've learnt from experience?

时光总嘲笑我的痴心妄想 提交于 2019-12-20 07:58:29
问题 What are some C++ related idioms, misconceptions, and gotchas that you've learnt from experience? An example: class A { public: char s[1024]; char *p; A::A() { p = s; } void changeS() const { p[0] = 'a'; } }; Even know changeS is a const member function, it is changing the value of the object. So a const member function only means that it will treat all variables as const, and it does not mean that it will actually keep all members const. (why? the const keyword on the member function treats

Is working past the end of a slice idiomatic?

扶醉桌前 提交于 2019-12-20 03:12:57
问题 I was reading through Go's compress/flate package, and I found this odd piece of code [1]: n := int32(len(list)) list = list[0 : n+1] list[n] = maxNode() In context, list is guaranteed to be pointing to an array with more data after. This is a private function, so it can't be misused outside the library. To me, this seems like a scary hack that should be a runtime exception. For example, the following D code generates a RangeError: auto x = [1, 2, 3]; auto y = x[0 .. 2]; y = y[0 .. 3];

Why does such a struct contain two array fields containing only one element?

可紊 提交于 2019-12-19 05:33:53
问题 Please Note: This question is not a duplicate of ( One element array in struct ) The following code is excerpted from the Linux kernel source (version: 3.14) struct files_struct { atomic_t count; struct fdtable __rcu *fdt; struct fdtable fdtab; spinlock_t file_lock ____cacheline_aligned_in_smp; int next_fd; unsigned long close_on_exec_init[1]; unsigned long open_fds_init[1]; struct file __rcu * fd_array[NR_OPEN_DEFAULT]; }; I just wonder why close_on_exec_init and open_fds_init are defined as

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

北城以北 提交于 2019-12-19 05:13:23
问题 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