idioms

Why and when should a comma be used at the end of a block?

纵饮孤独 提交于 2019-12-19 03:22:24
问题 There many cases in Rust when a block of code can end with or without comma. For example: enum WithoutComma { x, y } or enum WithComma { x, y, } There are also other examples with match , etc. It seems that both variants lead to the same result. The only case I know where adding or removing a comma changes behaviour is the 1-element tuple declaration (which isn't a block): let just_int = (5); let tuple = (5,); Why can one use a comma or not at the end of a block? Why is there such dualism in

Why and when should a comma be used at the end of a block?

核能气质少年 提交于 2019-12-19 03:22:21
问题 There many cases in Rust when a block of code can end with or without comma. For example: enum WithoutComma { x, y } or enum WithComma { x, y, } There are also other examples with match , etc. It seems that both variants lead to the same result. The only case I know where adding or removing a comma changes behaviour is the 1-element tuple declaration (which isn't a block): let just_int = (5); let tuple = (5,); Why can one use a comma or not at the end of a block? Why is there such dualism in

Why Clojure idiom prefer to return nil instead of empty list like Scheme?

强颜欢笑 提交于 2019-12-18 14:48:06
问题 From a comment on another question, someone is saying that Clojure idiom prefers to return nil rather than an empty list like in Scheme. Why is that? Like, (when (seq lat) ...) instead of (if (empty? lat) '() ...) 回答1: I can think of a few reasons: Logical distinction . In Clojure nil means nothing / absence of value. Whereas '() "the empty list is a value - it just happens to be a value that is an empty list. It's quite often conceptually and logically useful to distinguish between the two.

pimpl for a templated class

試著忘記壹切 提交于 2019-12-18 12:54:22
问题 I want to use the pimpl idiom to avoid having users of my library need our external dependencies (like boost, etc) however when my class is templated that seems to be impossible because the methods must be in the header. Is there something I can do instead? 回答1: If the class is templated, your users essentially need to compile it (and this is literally true in the most widely-used C++ implementations) and so they need your external dependencies. The simplest solution is to put the bulk of

Hashes of Hashes Idiom in Ruby?

≯℡__Kan透↙ 提交于 2019-12-17 21:47:03
问题 Creating hashes of hashes in Ruby allows for convenient two (or more) dimensional lookups. However, when inserting one must always check if the first index already exists in the hash. For example: h = Hash.new h['x'] = Hash.new if not h.key?('x') h['x']['y'] = value_to_insert It would be preferable to do the following where the new Hash is created automatically: h = Hash.new h['x']['y'] = value_to_insert Similarly, when looking up a value where the first index doesn't already exist, it would

Swift idiomatic error checking

…衆ロ難τιáo~ 提交于 2019-12-17 19:07:02
问题 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! 回答1:

Named Parameter idiom in Java

我怕爱的太早我们不能终老 提交于 2019-12-17 04:43:10
问题 How to implement Named Parameter idiom in Java? (especially for constructors) I am looking for an Objective-C like syntax and not like the one used in JavaBeans. A small code example would be fine. Thanks. 回答1: The best Java idiom I've seem for simulating keyword arguments in constructors is the Builder pattern, described in Effective Java 2nd Edition. The basic idea is to have a Builder class that has setters (but usually not getters) for the different constructor parameters. There's also a

Merge keys array and values array into an object in Javascript

大城市里の小女人 提交于 2019-12-17 04:30:40
问题 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? 回答1: 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

Why not a two-process state machine in VHDL?

主宰稳场 提交于 2019-12-14 00:32:30
问题 When I learnt how to express finite state machines in VHDL, it was with a two-process architecture. One process handles the clock/reset signals, and another handles the combinatorial logic of updating the state and output. An example is below. I've seen this style criticised (see the comments and answer to this question for example), but never in any detail. I'd like to know whether there are objective(ish) reasons behind this. Are there technical reasons to avoid this style? Xilinx'

Setting variables with blocks in ruby

雨燕双飞 提交于 2019-12-13 15:55:09
问题 I find myself using PHP-like loops a lot in Ruby and it feels wrong when the rest of the language is so neat. I wind up with code like this: conditions_string = '' zips.each_with_index do |zip, i| conditions_string << ' OR ' if i > 0 conditions_string << "npa = ?" end # Now I can do something with conditions string I feel like I should be able to do something like this conditions_string = zips.each_with_index do |zip, i| << ' OR ' if i > 0 << "npa = ?" end Is there a 'Neat' way to set a