syntactic-sugar

Why would you use “AS” when aliasing a SQL table?

蹲街弑〆低调 提交于 2019-11-27 23:33:59
I just came across a SQL statement that uses AS to alias tables, like this: SELECT all, my, stuff FROM someTableName AS a INNER JOIN someOtherTableName AS b ON a.id = b.id What I'm used to seeing is: SELECT all, my, stuff FROM someTableName a INNER JOIN someOtherTableName b ON a.id = b.id I'm assuming there's no difference and it's just syntactic sugar, but which of these is more prevalent/wide-spread? Is there any reason to prefer one over the other? Edited to clarify: I appreciate all the answers and all the points made, but the question was not why or why not to use table aliases. The

Scala single method interface implementation

喜夏-厌秋 提交于 2019-11-27 15:27:57
Does Scala have any syntactic sugar to replace the following code: val thread = new Thread(new Runnable { def run() { println("hello world") } }) with something more like: val thread = new Thread(() => println("hello world")) in cases when the trait/interface needs only one method to be implemented? If not, is there any chance to have this feature in Scala in the future? It is especially useful when one deals with Java classes. I found a similar question asked three years ago: Generically implementing a Java Single-Abstract-Method interface with a Scala closure? The answer says we should have

Python assert — improved introspection of failure?

人盡茶涼 提交于 2019-11-27 14:52:35
问题 This is a rather useless assertion error; it does not tell the values of the expression involved (assume constants used are actually variable names): $ python -c "assert 6-(3*2)" [...] AssertionError Is there a better assert implementation in Python that is more fancy? It must not introduce additional overhead over execution (except when assert fails) .. and must turn off if -O flag is used. Edit : I know about assert's second argument as a string. I don't want to write one .. as that is

Java in operator

守給你的承諾、 提交于 2019-11-27 12:46:24
问题 For the one millionth time, I would have liked to use an IN operator in Java, similar to the IN operator in SQL. It could just be implemented as compiler syntactic sugar. So this if (value in (a, b, c)) { } else if (value in (d, e)) { } ...would really be awesome. In fact, the above is the same as the rather verbose (and not adapted for primitives) construct here: if (Arrays.asList(a, b, c).contains(value)) { } else if (Arrays.asList(d, e).contains(value)) { } Or like this for int , long and

What is the best or most interesting use of Extension Methods you've seen? [closed]

别说谁变了你拦得住时间么 提交于 2019-11-27 09:57:31
I'm starting to really love extension methods... I was wondering if anyone her has stumbled upon one that really blew their mind, or just found clever. An example I wrote today: Edited due to other users' comments: public static IEnumerable<int> To(this int fromNumber, int toNumber) { while (fromNumber < toNumber) { yield return fromNumber; fromNumber++; } } This allows a for loop to be written as a foreach loop: foreach (int x in 0.To(16)) { Console.WriteLine(Math.Pow(2, x).ToString()); } I can't wait to see other examples! Enjoy! The full solution is too large to put here, but I wrote a

Is there a literal notation for an array of symbols?

泄露秘密 提交于 2019-11-27 09:38:52
问题 I like this literal expression for an array of strings: %w( i can easily create arrays of words ) I am wondering if there is a literal to get an array of symbols. I know I can do %w( it is less elegant to create arrays of symbols ).map( &:to_sym ) but it would be so wonderful just to use a literal. 回答1: Yes! This is possible now in Ruby 2.0.0. One way to write it is: %i{foo bar} # => [:foo, :bar] You can also use other delimiters, so you could also write %i(foo bar) or %i!foo bar! for example

Using a block's return value in JavaScript

自古美人都是妖i 提交于 2019-11-27 08:55:04
On a lot of browsers I've tested, JavaScript blocks actually return a value. You can test it out in any console: for(var i = 0; i < 10; i++) { var sqrt = Math.sqrt(i); if(Math.floor(sqrt) === sqrt) { i; } } The "return" value is the last square number, that is, 9! But since it isn't an expression I suppose, you can't do this: for(var i = 0; i < 10; i++) { ... } + 5 That doesn't work. It gives + 5 , or 5 , of course, because it's a separate statement. Putting the loop in parentheses obviously fails, and if a block is in parentheses (e.g. ({f(); r}) - doesn't work) it's treated as an object and

What are all the instances of syntactic sugar in Scala?

依然范特西╮ 提交于 2019-11-27 05:54:49
What are all the instances of syntactic sugar in Scala? They are hard to search for since most/all of them are purely symbols and are thus hard to search for without knowing the name of the concept. TODO: Implicit conversions _ syntax for anonymous functions Other things I'm forgetting Jackson Davis Basics: a b is equivalent to a.b a b c is equivalent to a.b(c) , except when b ends in : . In that case, a b c is equivalent to c.b(a) a(b) is equivalent to a.apply(b) This is why the following definitions for an anonymous functions are identical: val square1 = (x: Int) => x x val square2 = new

Magic First and Last Indicator in a Loop in Ruby/Rails?

廉价感情. 提交于 2019-11-27 05:21:04
问题 Ruby/Rails does lots of cool stuff when it comes to sugar for basic things, and I think there's a very common scenario that I was wondering if anyone has done a helper or something similar for. a = Array.new(5, 1) a.each_with_index do |x, i| if i == 0 print x+1 elsif i == (a.length - 1) print x*10 else print x end end Pardon the ugliness, but this gets at what one might want... is there a ruby way to do something to the first and last of a loop? [EDIT] I think ideally this would be an

C# property and ref parameter, why no sugar?

孤人 提交于 2019-11-27 00:59:48
I just ran across this error message while working in C# A property or indexer may not be passed as an out or ref parameter I known what caused this and did the quick solution of creating a local variable of the correct type, calling the function with it as the out / ref parameter and then assigning it back to the property: RefFn(ref obj.prop); turns into { var t = obj.prop; RefFn(ref t); obj.prop = t; } Clearly this would fail if the property doesn't support get and set in the current context. Why doesn't C# just do that for me? The only cases where I can think of where this might cause