idioms

Is Initialization On Demand Holder idiom thread safe without a final modifier

给你一囗甜甜゛ 提交于 2019-11-30 14:03:09
问题 I have a hunch that using the holder idiom without declaring the holder field as final is not thread safe (due to the way immutability works in Java). Can somebody confirm this (hopefully with some sources)? public class Something { private long answer = 1; private Something() { answer += 10; answer += 10; } public int getAnswer() { return answer; } private static class LazyHolder { // notice no final private static Something INSTANCE = new Something(); } public static Something getInstance()

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

耗尽温柔 提交于 2019-11-30 11:50:10
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) '() ...) mikera 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. Fit with JVM - the JVM object model supports null references. And quite a lot of Java APIs return

String concatenation with Groovy

此生再无相见时 提交于 2019-11-30 10:14:11
问题 What is the best (idiomatic) way to concatenate Strings in Groovy? Option 1: calculateAccountNumber(bank, branch, checkDigit, account) { bank + branch + checkDigit + account } Option 2: calculateAccountNumber(bank, branch, checkDigit, account) { "$bank$branch$checkDigit$account" } I've founded an interesting point about this topic in the old Groovy website: Things you can do but better leave undone. As in Java, you can concatenate Strings with the "+" symbol. But Java only needs that one of

Multiple Exits From F# Function

孤者浪人 提交于 2019-11-30 08:45:00
I could do this easily in C++ (note: I didn't test this for correctness--it's only to illustrate what I'm trying to do): const int BadParam = -1; const int Success = 0; int MyFunc(int param) { if(param < 0) { return BadParam; } //normal processing return Success; } But I cannot figure out how to exit a routine early in F#. What I want to do is to exit the function on a bad input but continue if the input is ok. Am I missing some fundamental property of F# or am I approaching the problem in the wrong way since I'm just learning FP? Is a failwith my only option here? This is what I've got so far

pimpl for a templated class

孤者浪人 提交于 2019-11-30 08:29:32
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? 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 your class's implementation in a non-template base class (or encapsulated member object of some class). Solve

Is “regex” in modern programming languages really “context sensitive grammar”?

我只是一个虾纸丫 提交于 2019-11-30 08:22:26
问题 Over the years, "regex" pattern matching has been getting more and more powerful to the point where I wonder: is it really just context-sensitive-grammar matching? Is it a variation/extension of context-free-grammar matching? Where is it right now and why don't we just call it that instead of the old, restrictive "regular expression"? 回答1: In particular backreferences to capturing parentheses make regular expressions more complex than regular, context-free, or context-sensitive grammars. The

What is “sentry object” in C++?

末鹿安然 提交于 2019-11-30 06:46:13
I answered this question , and Potatoswatter answered too as The modern C++ equivalent would be a sentry object: construct it at the beginning of a function, with its constructor implementing call(), and upon return (or abnormal exit), its destructor implements I am not familiar with using sentry objects in C++. I thought they were limited to input and output streams. Could somebody explain to me about C++ sentry objects as well as how to use them as an around interceptor for one or more methods in a class ? i.e. How to do this ? Sentry objects are very similar indeed. On the one hand they

What is the template<typename T, T t> idiom?

百般思念 提交于 2019-11-30 06:06:41
I was reading this and was trying to understand what N3601 was about. It said this idiom comes up a lot in a web search, but I couldn't find anything. What is the template<typename T, T t> idiom, what does it solve, how is it used, what is are Implicit template parameters, and what does the proposal aim to fix? The problem that is being solved is deducing types from template non-type parameters. Given: template<typename T> void foo(T); template<typename T, T> void bar(); it is possible to deduce T for foo (for example, foo(10) will result in T being deduced to be int ), but it is not possible

Python indentation in “empty lines”

早过忘川 提交于 2019-11-30 06:04:06
Which is preferred ("." indicating whitespace)? A) def foo(): x = 1 y = 2 .... if True: bar() B) def foo(): x = 1 y = 2 if True: bar() My intuition would be B (that's also what vim does for me), but I see people using A) all the time. Is it just because most of the editors out there are broken? The PEP 8 does not seem to be clear on this issue, although the statements about "blank lines" could be interpreted in favor of B. The PEP 8 style-checker (pep8.py) prefers B and warns if you use A; however, both variations are legal. My own view is that since Python will successfully interpret the code

idioms for returning multiple values in shell scripting

折月煮酒 提交于 2019-11-30 05:59:58
Are there any idioms for returning multiple values from a bash function within a script? http://tldp.org/LDP/abs/html/assortedtips.html describes how to echo multiple values and process the results (e.g., example 35-17), but that gets tricky if some of the returned values are strings with spaces in. A more structured way to return would be to assign to global variables, like foo () { FOO_RV1="bob" FOO_RV2="bill" } foo echo "foo returned ${FOO_RV1} and ${FOO_RV2}" I realize that if I need re-entrancy in a shell script I'm probably doing it wrong, but I still feel very uncomfortable throwing