idiomatic

Proper way to dynamically add functions to ES6 classes

柔情痞子 提交于 2019-12-03 19:01:07
问题 I have a simple class with a single method exec(arg1,..,argn) and I want to have a number of alias methods which call exec with predefined argument values (e.g. exec_sync = exec.bind(this, true) ). The following does the trick: class Executor { constructor() { this.exec_sync = this.exec.bind(this, true); } exec(sync, cmd, args/* ... */) { // impl } } But I don't know if this is a good idea or if this is idiomatic to ES6. UDATE: In a real-life example I have two nested loops with respectively

How to learn to write idiomatic c++ code [closed]

拜拜、爱过 提交于 2019-12-03 13:19:33
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . I recently forced myself to study C++ and I just finished reading the book C++: The Complete Reference, by Herbert Schildt. I liked the book and think I got more or less the big picture. I noticed though that when I try to check with other people the things I code using the material I learned, they are usually

How to count non-null elements in an iterable?

一笑奈何 提交于 2019-12-03 10:43:34
I'm looking for a better/more Pythonic solution for the following snippet count = sum(1 for e in iterable if e) len(filter(None, iterable)) Using None as the predicate for filter just says to use the truthiness of the items. (maybe clearer would be len(filter(bool, iterable)) ) Honestly, I can't think of a better way to do it than what you've got. Well, I guess people could argue about "better," but I think you're unlikely to find anything shorter, simpler, and clearer. Most Pythonic is to write a small auxiliary function and put it in your trusty "utilities" module (or submodule of

Is static_cast<T>(-1) the right way to generate all-one-bits data without numeric_limits?

家住魔仙堡 提交于 2019-12-03 10:41:01
问题 I'm writing C++ code in an environment in which I don't have access to the C++ standard library, specifically not to std::numeric_limits . Suppose I want to implement template <typename T> constexpr T all_ones( /* ... */ ) Focusing on unsigned integral types, what do I put there? Specifically, is static_cast<T>(-1) good enough? (Other types I could treat as an array of unsigned chars based on their size I guess.) 回答1: Use the bitwise NOT operator ~ on 0 . T allOnes = ~(T)0; A static_cast<T>(

What's the closest thing in C++ to retroactively defining a superclass of a defined class?

对着背影说爱祢 提交于 2019-12-03 08:14:33
问题 Suppose I have the class class A { protected: int x,y; double z,w; public: void foo(); void bar(); void baz(); }; defined and used in my code and the code of others. Now, I want to write some library which could very well operate on A's, but it's actually more general, and would be able to operate on: class B { protected: int y; double z; public: void bar(); }; and I do want my library to be general, so I define a B class and that's what its APIs take. I would like to be able to tell the

Finding an item that matches predicate in Scala

旧时模样 提交于 2019-12-03 06:13:51
问题 I'm trying to search a scala collection for an item in a list that matches some predicate. I don't necessarily need the return value, just testing if the list contains it. In Java, I might do something like: for ( Object item : collection ) { if ( condition1(item) && condition2(item) ) { return true; } } return false; In Groovy, I can do something like: return collection.find { condition1(it) && condition2(it) } != null What's the idiomatic way to do this in Scala? I could of course convert

How to learn to write idiomatic c++ code [closed]

六月ゝ 毕业季﹏ 提交于 2019-12-03 02:40:34
I recently forced myself to study C++ and I just finished reading the book C++: The Complete Reference, by Herbert Schildt. I liked the book and think I got more or less the big picture. I noticed though that when I try to check with other people the things I code using the material I learned, they are usually considered non-idiomatic and superseded by an STL way to do it that is safer and easier (well, the book doesn't cover STL and Boost libraries). So I'd like to ask: what are good sources to learn the patterns of a good C++ program? Where can I learn basic patterns from the "C++ way" to do

Is static_cast<T>(-1) the right way to generate all-one-bits data without numeric_limits?

拜拜、爱过 提交于 2019-12-03 01:11:55
I'm writing C++ code in an environment in which I don't have access to the C++ standard library, specifically not to std::numeric_limits . Suppose I want to implement template <typename T> constexpr T all_ones( /* ... */ ) Focusing on unsigned integral types, what do I put there? Specifically, is static_cast<T>(-1) good enough? (Other types I could treat as an array of unsigned chars based on their size I guess.) Leandros Use the bitwise NOT operator ~ on 0 . T allOnes = ~(T)0; A static_cast<T>(-1) assumes two's complement, which is not portable. If you are only concerned about unsigned types,

What's the closest thing in C++ to retroactively defining a superclass of a defined class?

a 夏天 提交于 2019-12-02 21:56:06
Suppose I have the class class A { protected: int x,y; double z,w; public: void foo(); void bar(); void baz(); }; defined and used in my code and the code of others. Now, I want to write some library which could very well operate on A's, but it's actually more general, and would be able to operate on: class B { protected: int y; double z; public: void bar(); }; and I do want my library to be general, so I define a B class and that's what its APIs take. I would like to be able to tell the compiler - not in the definition of A which I no longer control, but elsewhere, probably in the definition

Finding an item that matches predicate in Scala

纵饮孤独 提交于 2019-12-02 18:42:45
I'm trying to search a scala collection for an item in a list that matches some predicate. I don't necessarily need the return value, just testing if the list contains it. In Java, I might do something like: for ( Object item : collection ) { if ( condition1(item) && condition2(item) ) { return true; } } return false; In Groovy, I can do something like: return collection.find { condition1(it) && condition2(it) } != null What's the idiomatic way to do this in Scala? I could of course convert the Java loop style to Scala, but I feel like there's a more functional way to do this. Use filter: