idioms

Idiomatic use of std::rel_ops

谁说胖子不能爱 提交于 2019-11-27 00:51:15
问题 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

`if key in dict` vs. `try/except` - which is more readable idiom?

非 Y 不嫁゛ 提交于 2019-11-27 00:45:42
I have a question about idioms and readability, and there seems to be a clash of Python philosophies for this particular case: I want to build dictionary A from dictionary B. If a specific key does not exist in B, then do nothing and continue on. Which way is better? try: A["blah"] = B["blah"] except KeyError: pass or if "blah" in B: A["blah"] = B["blah"] "Do and ask for forgiveness" vs. "simplicity and explicitness". Which is better and why? Exceptions are not conditionals. The conditional version is clearer. That's natural: this is straightforward flow control, which is what conditionals are

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

可紊 提交于 2019-11-27 00:15:22
问题 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

Rails idiom to avoid duplicates in has_many :through

老子叫甜甜 提交于 2019-11-26 23:58:24
问题 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

C++ code for state machine

左心房为你撑大大i 提交于 2019-11-26 23:49:53
问题 This was an interview question to be coded in C++: Write code for a vending machine: Start with a simple one where it just vends one type of item. So two state variables: money and inventory, would do. My answer: I would use a state machine which has about 3-4 states. Use an enum variable to indicate the state and use a switch case statement, where each case has the operations to be done corresponding to each state and stay in a loop to move from one state to another. The next question: But

What is the pythonic way to detect the last element in a 'for' loop?

蓝咒 提交于 2019-11-26 23:37:05
I'd like to know the best way (more compact and "pythonic" way) to do a special treatment for the last element in a for loop. There is a piece of code that should be called only between elements, being suppressed in the last one. Here is how I currently do it: for i, data in enumerate(data_list): code_that_is_done_for_every_element if i != len(data_list) - 1: code_that_is_done_between_elements Is there any better way? Note: I don't want to make it with hacks such as using reduce . ;) Most of the times it is easier (and cheaper) to make the first iteration the special case instead of the last

Python “Every Other Element” Idiom [duplicate]

最后都变了- 提交于 2019-11-26 21:54:12
This question already has an answer here: Iterating over every two elements in a list 18 answers I feel like I spend a lot of time writing code in Python, but not enough time creating Pythonic code. Recently I ran into a funny little problem that I thought might have an easy, idiomatic solution. Paraphrasing the original, I needed to collect every sequential pair in a list. For example, given the list [1,2,3,4,5,6] , I wanted to compute [(1,2),(3,4),(5,6)] . I came up with a quick solution at the time that looked like translated Java. Revisiting the question, the best I could do was l = [1,2,3

Most idiomatic way to print a time difference in Java?

∥☆過路亽.° 提交于 2019-11-26 21:04:40
I'm familiar with printing time difference in milliseconds: long time = System.currentTimeMillis(); //do something that takes some time... long completedIn = System.currentTimeMillis() - time; But, is there a nice way print a complete time in a specified format (eg: HH:MM:SS) either using Apache Commons or even the dreaded platform API's Date/Time objects? In other words, what is the shortest, simplest, no nonsense way to write a time format derived from milliseconds in Java? Apache Commons has the DurationFormatUtils class for applying a specified format to a time duration. So, something like

An interesting C linked list idiom

北城余情 提交于 2019-11-26 19:26:17
问题 I was at an interview for a C position in which they presented me with an idiom that I haven't previously encountered. This is a trick that simplifies implementation of various algorithms involving linked lists and I'm wondering if anybody else has encountered this. Say we have a linked list record defined so: typedef struct _record { char* value; struct _record* next; } record; We need a function that inserts a new record so that the entire list remains sorted with respect to the value's in

Merge keys array and values array into an object in Javascript

点点圈 提交于 2019-11-26 18:58:14
I have: var keys = [ "height", "width" ]; var values = [ "12px", "24px" ]; And I'd like to convert it into this object: { height: "12px", width: "24px" } In Python, there's the simple idiom dict(zip(keys,values)) . Is there something similar in jQuery or plain Javascript, or do I have to do this the long way? Simple JS function would be: function toObject(names, values) { var result = {}; for (var i = 0; i < names.length; i++) result[names[i]] = values[i]; return result; } Of course you could also actually implement functions like zip, etc as JS supports higher order types which make these