idioms

What C++ idioms should C++ programmers use? [closed]

懵懂的女人 提交于 2019-11-28 15:09:00
What C++ idioms should C++ programmers know? By C++ idioms, I mean design patterns or way of doing certain things that are only applicable for C++ or more applicable for C++ than most other languages. Why one should use the idioms, and what do the idioms accomplish? Here is one list . If I had to pick a couple I might go with the Curiously Recurring Template Pattern or Virtual Contstructors. By far the single most important "pattern" to learn and know that's (nearly) unique to C++ is RAII (Resource Acquisition Is Initialization). Edit: (To answer extra question edited into the question). You

Haskell: Is there an idiomatic way to insert every item in a list into its own list?

≡放荡痞女 提交于 2019-11-28 13:41:10
I've been using ((:[]) <$> xs) but if there is a more clear way I would love to use it. edit: so many good answers guys! I don't think I can accept one because they are all good. I believe map return or map pure are good enough. Maybe this? map (\x -> [x]) xs Yours can work on any functor I think so this would be more idomatic for just lists. The split package provides a (Data.List.Split.)chunksOf function whose name is, IMO, more meaningful than the various map solutions (even if they are more idiomatic.) You can also use a list comprehension: [ [x] | x <- theList] Maybe overkill for such a

Swift idiomatic error checking

自作多情 提交于 2019-11-28 09:33:09
Let's say that you have a function like this: func getSomething(error: NSErrorPointer) -> Something and you typically use it this way: var error : NSError? = nil let a = getSomething(&error) What is an idiomatic way to check for error here? More specific questions: If error == nil can we assume that a will never be nil and vice versa? What should we check first: error (for its nilness) or a (to confirm that it's not a nil)? Can a != nil && error != nil be true in some cases? Thank you! Compare Handling Error Objects Returned From Methods in the "Error Handling Programming Guide": Important:

Is str.replace(..).replace(..) ad nauseam a standard idiom in Python?

有些话、适合烂在心里 提交于 2019-11-28 06:56:51
问题 For instance, say I wanted a function to escape a string for use in HTML (as in Django's escape filter): def escape(string): """ Returns the given string with ampersands, quotes and angle brackets encoded. """ return string.replace('&', '&').replace('<', '<').replace('>', '>').replace("'", ''').replace('"', '"') This works, but it gets ugly quickly and appears to have poor algorithmic performance (in this example, the string is repeatedly traversed 5 times). What would be better is something

Idiomatic use of std::rel_ops

∥☆過路亽.° 提交于 2019-11-28 05:41:05
What is the preferred method of using std::rel_ops to add the full set of relational operators to a class? This documentation suggests a using namespace std::rel_ops , but this seems to be deeply flawed, as it would mean that including the header for the class implemented in this way would also add full relational operators to all other classes with a defined operator< and operator== , even if that was not desired. This has the potential to change the meaning of code in surprising ways. As a side note - I have been using Boost.Operators to do this, but I am still curious about the standard

How to implement an interface class using the non-virtual interface idiom in C++?

落爺英雄遲暮 提交于 2019-11-28 05:07:51
问题 In C++ an interface can be implemented by a class with all its methods pure virtual. Such a class could be part of a library to describe what methods an object should implement to be able to work with other classes in the library: class Lib::IFoo { public: virtual void method() = 0; }; : class Lib::Bar { public: void stuff( Lib::IFoo & ); }; Now I want to to use class Lib::Bar, so I have to implement the IFoo interface. For my purposes I need a whole of related classes so I would like to work

Rails 3: What is the proper way to respond to REST-ful actions with JSON in rails?

让人想犯罪 __ 提交于 2019-11-28 04:01:19
I'm trying to make an API for my rails application using JSON responses to RESTful resource controllers. This is a new experience for me, so I'm looking for some guidance and pointers. To start things off: In a rails application, what is the "proper" way to respond with JSON to REST-ful controller methods? (create, update, destroy) Is there an idiomatic way to indicate success/failure through a JSON response? Additional information: I'm currently working with rails 3.0.beta2 I would like to avoid using a plugin or gem to do the grunt work, my goal is to gain a better understanding of how to

Rails idiom to avoid duplicates in has_many :through

狂风中的少年 提交于 2019-11-28 03:12:18
I have a standard many-to-many relationship between users and roles in my Rails app: class User < ActiveRecord::Base has_many :user_roles has_many :roles, :through => :user_roles end I want to make sure that a user can only be assigned any role once. Any attempt to insert a duplicate should ignore the request, not throw an error or cause validation failure. What I really want to represent is a "set", where inserting an element that already exists in the set has no effect. {1,2,3} U {1} = {1,2,3}, not {1,1,2,3}. I realize that I can do it like this: user.roles << role unless user.roles.include?

Common Ruby Idioms

纵饮孤独 提交于 2019-11-28 02:34:13
One thing I love about ruby is that mostly it is a very readable language (which is great for self-documenting code) However, inspired by this question: Ruby Code explained and the description of how ||= works in ruby, I was thinking about the ruby idioms I don't use, as frankly, I don't fully grok them. So my question is, similar to the example from the referenced question, what common, but not obvious, ruby idioms do I need to be aware of to be a truly proficient ruby programmer? By the way, from the referenced question a ||= b is equivalent to if a == nil || a == false a = b end (Thanks to

Access operator functions by symbol

喜夏-厌秋 提交于 2019-11-28 01:36:13
问题 I need a function which takes one of python's operator symbols or keywords as a string , along with its operands, evaluates it, and returns the result. Like this: >>> string_op('<=', 3, 3) True >>> string_op('|', 3, 5) 7 >>> string_op('and', 3, 5) True >>> string_op('+', 5, 7) 12 >>> string_op('-', -4) 4 The string cannot be assumed to be safe. I will be satisfied with just mapping the binary operators, but I'd be extra happy if I could get all of them. My current implementation manually maps