syntactic-sugar

Is <boolean expression> && statement() the same as if(<boolean expression>) statement()?

∥☆過路亽.° 提交于 2019-12-17 02:32:07
问题 Are the two identical? Suppose you have: var x = true; And then you have one of either: x && doSomething(); or if(x) doSomething(); Is there any differene whatsoever between the two syntaxes? Did I stumble across a nice bit of sugar? 回答1: Strictly speaking, they will produce the same results, but if you use the former case as a condition for something else, you will get dissimilar results. This is because in the case of x && doSomething() , doSomething() will return a value to signify its

Double Brace initialization and serialization

无人久伴 提交于 2019-12-13 16:00:55
问题 I've noticed a strange behavior, when using double brace initialization, the initialized object serialization fails : queueVO.setUser(new UserVO() {{setIndex("admin");}}); result in the following error when sending the object to a JMS queue : javax.jms.JMSException: Failed to serialize object at org.hornetq.jms.client.HornetQObjectMessage.setObject(HornetQObjectMessage.java:139) whereas everything is running fine otherwise queueVO.setUser(new UserVO()); queueVO.getUser().setIndex("admin"); I

Kotlin apply on String not working as expected

匆匆过客 提交于 2019-12-13 03:15:32
问题 I am trying to use all features of kotlin, but seems not of them are working, or may be it's my fault. So, apply to String not work. Example: val str = someStr.apply { toUpperCase() if (contains("W")) replace("W", "w") } Input -> xywz Output -> xywz Expected -> XYwZ Java style: val str = it.text().toString().toUpperCase() if (str.contains("W")) str.replace("W", "w") Input -> xywz Output -> XYwZ Expected -> XYwZ Am I doing something wrong? 回答1: toUpperCase() returns a copy of the string

java sugaring, can I avoid almost-duplicate code here?

不问归期 提交于 2019-12-12 10:52:25
问题 private class HSV extends HorizontalScrollView { public LinearLayout L; public AbsoluteLayout A; public HSV(Context context) { super(context); L = new LinearLayout(context); A = new AbsoluteLayout(context); } @Override public void addView(View child) { A.addView(child); } void update_scroll() { removeView(L); addView(L, 0); L.removeView(A); L.addView(A); A.invalidate(); L.invalidate(); invalidate(); requestLayout(); } int GetCurrentPos() { return getScrollX(); // <-- this line if HSV return

implementing apply function in Rcpp

耗尽温柔 提交于 2019-12-12 09:39:43
问题 I have been trying to implement apply function in Rcpp so far the code looks like this //[[Rcpp::export]] NumericVector apply(NumericMatrix x,int dim,Function f){ NumericVector output; if(dim==1){ for(int i=0;i<x.nrow();i++){ output[i]=f(x(i,_)); } } else if(dim==2){ for(int i=0;i<x.ncol();i++){ output[i]=f(x(_,i)); } } return(output); } but i'm getting an error "cannot convert SEXP to double in assignment" in line 6 and 11. Is there any way to convert the value returned by an arbitrary

Order of arguments and pipe-right operator

☆樱花仙子☆ 提交于 2019-12-11 18:24:35
问题 Is there a way to simplify the following, so I won't need a runWithTimeout function? let runWithTimeout timeout computation = Async.RunSynchronously(computation, timeout) let processOneItem item = fetchAction item |> runWithTimeout 2000 Edit: Here's why i needed the extra method: let processOneItem item = fetchAction item |> Async.Catch |> runWithTimeout 2000 |> handleExceptions 回答1: Perhaps you mean something like this: let processOneItem item = fetchAction item |> fun x -> Async

Postgres implicitly shaped temporary tables. Syntactic sugar I am not aware of?

寵の児 提交于 2019-12-11 17:33:59
问题 I'm writing a batch mode update function and at the moment it looks like this : CREATE OR REPLACE FUNCTION gen_category_counts() RETURNS VOID as $$ BEGIN CREATE TEMPORARY TABLE category_stats ON COMMIT DROP AS WITH PC as( SELECT category_id, COUNT(*) FROM forum_posts fp INNER JOIN forum_topics ft ON ft.forum_id = fp.forum_id AND ft.id = fp.topic_id GROUP BY category_id ), tc as( SELECT category_id, COUNT(*) FROM forum_topics GROUP BY category_id ) SELECT tc.category_id,tc.count AS topic_count

what is does it mean println(_)?

帅比萌擦擦* 提交于 2019-12-11 11:23:56
问题 I have this piece of code in scala val wordCounts = logData.flatMap(line => line.split(" ")).map(word => (word, 1)).reduceByKey((a, b) => a + b) wordCounts.foreach(println(_)) So what does println(_) mean and what should it print? 回答1: As explained in the section "Placeholder Syntax for Anonymous Functions" of the Spec, println(_) is a shortcut for the anonymous function literal w => println(w) which in turn is a shortcut for something like (w: (String, Int)) => println(w) in this particular

Why Are Parentheses Required on C# Static Constructors?

删除回忆录丶 提交于 2019-12-10 20:26:45
问题 Consider: class Foo { static Foo() { // Static initialisation } } Why are the () required in static Foo() {...} ? The static constructor must always be parameterless, so why bother? Are they necessary to avoid some parser ambiguity, or is it just to maintain consistency with regular parameterless constructors? Since it looks so much like an initialiser block, I often find myself leaving them out by accident and then have to think for a few seconds about what is wrong. It would be nice if they

Destructuring assignment - object properties to variables in C#

两盒软妹~` 提交于 2019-12-09 14:29:27
问题 JavaScript has a nifty feature where you can assign several variables from properties in an object using one concise line. It's called destructuring assignment syntax which was added in ES6. // New object var o = {p1:'foo', p2:'bar', p3: 'baz'}; // Destructure var {p1, p2} = o; // Use the variables... console.log(p1.toUpperCase()); // FOO console.log(p2.toUpperCase()); // BAR I want to do something similar with C#. // New anonymous object var o = new {p1="foo", p2="bar", p3="baz"}; //