idioms

When should I use the new ranged-for and can I combine it with the new cbegin/cend?

ぃ、小莉子 提交于 2019-12-30 08:02:08
问题 The new ranged-for in C++11 will be very concise and useful, of course. As far as I understand how it works, it looks up the "containers" begin and end by trying *Argument-Depending-Lookup" (ADT). But another addition is that all the containers now have cbegin() and cend() to get the const_iterators for the container. I am a bit confused, on the one hand I guess I should use cbegin() if I do not want to modify the container, on the other hand I have to add an additional const inside the

When should I use the new ranged-for and can I combine it with the new cbegin/cend?

隐身守侯 提交于 2019-12-30 08:01:07
问题 The new ranged-for in C++11 will be very concise and useful, of course. As far as I understand how it works, it looks up the "containers" begin and end by trying *Argument-Depending-Lookup" (ADT). But another addition is that all the containers now have cbegin() and cend() to get the const_iterators for the container. I am a bit confused, on the one hand I guess I should use cbegin() if I do not want to modify the container, on the other hand I have to add an additional const inside the

Sort a list of objects by using their attributes in Ruby

家住魔仙堡 提交于 2019-12-30 05:02:48
问题 I have a list of Fruit structs called basket . Each Fruit struct has a name (a string) and a calories (an integer). I would like to sort basket so that: The Fruit s with the highest calories appear first. For example, a fruit with 500 calories appears before a fruit with 400 calories. If two Fruit s have equal calories , the Fruit whose name comes first alphabetically comes first, ignoring case. For example, given two fruits with equal calories, one named "banana" will come before one named

Best Loop Idiom for special casing the last element

我只是一个虾纸丫 提交于 2019-12-29 10:36:09
问题 I run into this case a lot of times when doing simple text processing and print statements where I am looping over a collection and I want to special case the last element (for example every normal element will be comma separated except for the last case). Is there some best practice idiom or elegant form that doesn't require duplicating code or shoving in an if, else in the loop. For example I have a list of strings that I want to print in a comma separated list. (the do while solution

Is there a better way to write this named_scope? [Rails]

Deadly 提交于 2019-12-24 23:00:25
问题 I am using this named_scope to search for products that have a description matching any word the user inputs. E.g., Product.description_like_any("choc pret") Will return products with names like "Chocolate Bar" "Chocolate Covered Pretzels" "Miniature Chocolate Ponies" Here's the named_scope I've written (which works) named_scope :description_like_any, (lambda do |query| return {} unless query conditions = [] values = [] for q in query.split(/\s+/) conditions << "(`products`.description LIKE ?

Advice for multi level csv file creation

萝らか妹 提交于 2019-12-24 17:43:35
问题 I have some data that end user wants in a CSV file. Each entry has a parent node and zero or more child nodes. For example, a parent node might contain: Name, Id, Date While a child node might contain: Name, ChildId So what I am searching for is a standard to represent multi-level data in CSV. In XML, I can easily create sub nodes. What is the best way to do this in CSV? I want to create a script to extract this data without any confusion about what is parent data and what is child data. In

Checking Clojure pre-conditions without running the function?

大憨熊 提交于 2019-12-24 10:41:41
问题 I have one function that does some (possibly lengthy) work (defn workwork [x] ...) and some other functions to check if the call will succeed ahead of time (defn workwork-precondition-1 [x] ...) . The precondition functions should be evaluated every time workwork is called (e.g. using :pre ). The precondition functions should also be collected (and:ed) in a single function and made available to client code directly (e.g. to disable a button). Which is the idiomatic way to solve this in

templated Attorney-Client Idiom for more many classes

大憨熊 提交于 2019-12-24 10:37:34
问题 I was trying to apply the Attorney-Client Idiom (know as PassKey Idiom) and this for two classes. I explain : I have a class named Secret which contains name and age as private members. I have created a class Attorney which defines 2 getters for each memeber in class Secret . I want to create 2 classes: showAge which only have access to 1 getter showAgeAndName which has access to both getters. So far all works fine, but looking at the code, it not well maintainable : if I add new members to

Cannot override method and cannot access field while using idiom “Providing a default interface implementation”

时光怂恿深爱的人放手 提交于 2019-12-24 08:56:20
问题 Here is code: IDefaultInterface.aj: public interface IDefaultInterface { public void m1(); static aspect Impl{ public int f1; public void IDefaultInterface.m1(){ } } } DefaulstInterfaceClass.java: public class DefaultInterfaceClass implements IDefaultInterface { @Override public void m1() { } void mm() { f1 = 9; } } In the second piece of code I'm trying to override m1() method and access f1 field. The compiler allows neither one. How to overcome these limitations? Additional thoughts. I

D3.js: Is this good practise for data-DOM-binding?

可紊 提交于 2019-12-24 07:48:51
问题 I'm trying to grasp the D3 basics. I understand that it's crucial to get the binding right. So I did a simple web page with update, enter and exit operations. Every time the data changes (modified, added, removed) I call a function where the data is bound to svg-objects in the update, enter and exit ways. I wonder if my approach is a good one? <html> <head> <style> .drawarea { border-style: solid; } </style> <script src="https://d3js.org/d3.v4.min.js"></script> <script> var data, drawArea;