idiomatic

Aggregating values in a data frame based on key

99封情书 提交于 2019-12-12 08:28:46
问题 I've got a piece of aggregation code that works well enough but runs a bit slow against a data frame with 10e6 rows. I'm not that experienced in R so apologies for my cringe worthy code! I just want to do a basic roll up and sum of values for a common key... eg go from... key val 1 a 5 2 b 7 3 a 6 to... key val 1 a 11 2 b 7 the best i can manage is... keys = unique(inp$key) vals = sapply(keys, function(x) { sum(inp[inp$key==x,]$val) }) out = data.frame(key=keys, val=vals) I have this gut feel

Pythonic way of converting a list to inputs of a function of several variables [duplicate]

夙愿已清 提交于 2019-12-12 02:59:30
问题 This question already has answers here : unpacking function argument [duplicate] (2 answers) Closed 5 years ago . I'm beginning to feel a little dumb for not figuring this out... say I have a function of two variables: def testfun(x,y): return x*y now I have a list of values, say [2,3] , which I want to input as values into the function. What's the most Pythonic way to do this? I thought of something like this: def listfun(X): X_as_strs = str(X).strip('[]') expression = 'testfun({0})'.format

What is the idiomatic way to create a collection of references to methods that take self?

烈酒焚心 提交于 2019-12-11 12:11:08
问题 I am trying to convert a Python script into Rust as a learning experience and to make the tool faster and shrink the size of the code/executable. I'm currently trying to convert a section that creates a list of references to methods on self. Now I've learned that there isn't a way to bind the self variable for a method, one has to use a closure and close over the object that the methods will be called on. However when you create a closure it gets assigned a unique anonymous type, so I don't

Splitting complex String Pattern (without regex) in a functional approach

爷,独闯天下 提交于 2019-12-11 09:54:33
问题 I am trying to split a string without regex in a more idiomatic functional approach. case class Parsed(blocks: Vector[String], block: String, depth: Int) def processChar(parsed: Parsed, c: Char): Parsed = { import parsed._ c match { case '|' if depth == 0 => parsed.copy(block = "", blocks = blocks :+ block , depth = depth) case '[' => parsed.copy(block = block + c, depth = depth + 1) case ']' if depth == 1 => parsed.copy( block = "", blocks = blocks :+ (block + c), depth = depth - 1) case ']'

Working with different IEEE floating-point rounding modes in C++

前提是你 提交于 2019-12-11 07:31:15
问题 Woe is me, I have to ensure the same floating-point results on a GPU and on the CPU. Ok, I understand IEEE has taken care of me and provided a nice standard to adhere to with several rounding options; and the CUDA part is sorted out (there are intrinsics for the different rounding modes), so that's just motivation. But in host-side C++ code - how do I perform floating-point arithmetic in a specific rounding mode (and I mean in a specific statement, not throughout my translation unit)? Are

Using a pure function in a Haskell monad / left-lifting?

*爱你&永不变心* 提交于 2019-12-11 06:28:02
问题 Consider the following function: foo = [1,2,3] >>= return . (*2) . (+1) For better readability and logic, I would like to move my pure functions (*2) and (+1) to the left of the return. I could achieve this like this: infixr 9 <. (<.) :: (a -> b) -> (b -> c) -> (a -> c) (<.) f g = g . f bar = [1,2,3] >>= (+1) <. (*2) <. return However, I don't like the right-associativity of (<.) . Let's introduce a function leftLift : leftLift :: Monad m => (a -> b) -> a -> m b leftLift f = return . f baz =

Idiomatic Python - checking for zero

断了今生、忘了曾经 提交于 2019-12-11 06:16:54
问题 What is the idiomatic way for checking a value for zero in Python? if n == 0: or if not n: I would think that the first one is more in the spirit of being explicit but on the other hand, empty sequences or None is checked similar to the second example. 回答1: If you want to check for zero, you should use if n == 0 . Your second expression doesn't check for zero, it checks for any value that evaluates as false. I think the answer lies in your requirements; do you really want to check for zero,

List comprehension in R: map, not filter

筅森魡賤 提交于 2019-12-11 04:07:30
问题 So, this question tells how to perform a list comprehension in R to filter out new values. I'm wondering, what is the standard R way of writing a list comprehension which is generating new values? Basically, if we have a function f and vector x , I want the list f(el) for el in x . (This is like map in functional programming). In Python, this would just be [f(el) for el in x] . How do I write this in R, in the standard way? The problem is, right now I have for-loops: result = c(0) for (i in 1

A better idiom for referring to base classes from derived classes?

巧了我就是萌 提交于 2019-12-11 02:22:11
问题 Suppose I have a template <typename T> class A : class_with_long_name<T, and_many_other, template_arguments, oh_my_thats_long>, anotherclass_with_long_name<and_many_other, template_arguments_that, are_also_annoying, including_also, T> { ... } Now, in class A's definition, and/or in its methods, I need to refer to the two superclasses (e.g. to access members in the superclass, or types defined in it etc.) However, I want to avoid repeating the superclass names. At the moment, what I'm doing is

Scala/Java enumerations

三世轮回 提交于 2019-12-10 15:58:22
问题 I've read this and this, but I still don't understand the (idiomatic) equivalent way to do this in Scala enum Status { OK(1, "Ok", "Okay"), NOT_OK(5, "Not Ok", "Not Okay") BAD(10, "Bad", "Run for your life") int code; String name; String description; // custom fields Status(int code, String name, String description) { this.code = code; this.name = name; this.description = description; } } class Main { public static void main(String[] args) { for(Status status : Status.values) { // iterate