idioms

How to implement function with vector of derived classes as parameter

末鹿安然 提交于 2020-01-04 06:50:35
问题 (Related to a previous unanswered question I asked). I want to implement a function which can be called only with vectors of related classes as parameter. For eq if we have class A; class B: public A; class C: public A; class D then it should be possible to call function with vector<A*>,vector<B*> or vector <C*> but not vector <D*> Any suggestions 回答1: I guess you already tried to create a method like void doSomething(std::vector<A*>& things) { } and tried do pass std::vector<B*> bList = ...;

Thread-safe implementation of the Copy-on-write (COW) idiom?

十年热恋 提交于 2020-01-04 02:09:09
问题 Can anyone point me to a thread-safe implementation of the Copy-on-write (COW) idiom? The sample code on this site looks good -- is it thread-safe? In case anyone is wondering what I will be using it for: I have a Foo class that has a std::map<int,double> member. Foo objects are copied very frequently in my code, but the copies rarely modify the contained map . I found that COW gives me a 22% performance boost, compared to copying the whole map contents in the Foo copy constructor, but my COW

tips'n'tricks in awk [closed]

萝らか妹 提交于 2020-01-03 17:56:28
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . I'm looking for caveats, tips'n'tricks etc. for awk. For example: awk '$9=="404"{a[$7]++}END{for(i in a)print a[i],i}' access.log|less

What is the idiomatic way to have a private function tested?

大城市里の小女人 提交于 2020-01-03 15:23:52
问题 The Rust book says that using a "tests" module is the idiomatic way to have unit tests. But I cannot see a function from the super module in the tests module if that function is not marked 'pub'. How should one test internal functions then? My first instinct was to look for a way to #ifdef the keyword pub . I have done this in the past for C++ testing. For Rust what I have done is simply have tests for private functions in the module and then tests for the public interface in the "tests"

What is the idiomatic way to have a private function tested?

老子叫甜甜 提交于 2020-01-03 15:23:12
问题 The Rust book says that using a "tests" module is the idiomatic way to have unit tests. But I cannot see a function from the super module in the tests module if that function is not marked 'pub'. How should one test internal functions then? My first instinct was to look for a way to #ifdef the keyword pub . I have done this in the past for C++ testing. For Rust what I have done is simply have tests for private functions in the module and then tests for the public interface in the "tests"

Is there a Python shortcut for variable checking and assignment?

霸气de小男生 提交于 2020-01-03 09:04:24
问题 I'm finding myself typing the following a lot (developing for Django, if that's relevant): if testVariable then: myVariable = testVariable else: # something else Alternatively, and more commonly (i.e. building up a parameters list) if 'query' in request.POST.keys() then: myVariable = request.POST['query'] else: # something else, probably looking at other keys Is there a shortcut I just don't know about that simplifies this? Something with the kind of logic myVariable = assign_if_exists

best c# syntax/idiom, reading array of friends from Facebook

送分小仙女□ 提交于 2020-01-03 04:22:06
问题 In c#, I'm simply grabbing "/me/friends" from the FB api, private void FacebookFriends() { FB.API("/me/friends", HttpMethod.GET, FBAPIFriendsCallback); } private void FBAPIFriendsCallback(FBResult response) { // (error handling here - no problem) // ugly code... var dict = Json.Deserialize(response.Text) as Dictionary<string,object>; var friendList = new List<object>(); friendList = (List<object>)(dict["data"]); int _friendCount = friendList.Count; // ugly code... // (example shows getting

ruby default argument idiom

荒凉一梦 提交于 2020-01-02 01:19:11
问题 What's the idiom in Ruby when you want to have a default argument to a function, but one that is dependent on another parameter / another variable? For example, in Python, an example is: def insort_right(a, x, lo=0, hi=None): if hi is None: hi = len(a) while lo < hi: mid = (lo+hi)//2 if x < a[mid]: hi = mid else: lo = mid+1 a.insert(lo, x) Here, if hi is not supplied, it should be len(a) . You can't do len(a) in the default argument list, so you assign it a sentinel value, None, and check for

Preferred file line by line read idiom in Python

99封情书 提交于 2020-01-01 10:46:05
问题 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? 回答1: 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

How do I use Python to easily expand variables to strings?

拜拜、爱过 提交于 2020-01-01 02:34:23
问题 What's a nice idiom to do this: Instead of: print "%s is a %s %s that %s" % (name, adjective, noun, verb) I want to be able to do something to the effect of: print "{name} is a {adjective} {noun} that {verb}" 回答1: "{name} is a {adjective} {noun} that {verb}".format(**locals()) locals() gives a reference to the current namespace (as a dictionary). **locals() unpacks that dictionary into keyword arguments ( f(**{'a': 0, 'b': 1}) is f(a=0, b=1) ). .format() is "the new string formatting", which