syntactic-sugar

Best javascript syntactic sugar

Here are some gems: Literals: var obj = {}; // Object literal, equivalent to var obj = new Object(); var arr = []; // Array literal, equivalent to var arr = new Array(); var regex = /something/; // Regular expression literal, equivalent to var regex = new RegExp('something'); Defaults: arg = arg || 'default'; // if arg evaluates to ...

Is there syntactic sugar for binding a value inside an anonymous function in Scala?

Instead of writing ((x: Double) => (((y: Double) => y*y))(x+x))(3) I would like to write something like ((x: Double) => let y=x+x in y*y)(3) Is there anything like this sort of syntactic sugar in Scala? ...

Duck type as syntactic sugar for reflection: Good or bad idea?

I've been thinking lately, would it be a good form of syntactic sugar in languages like Java and C#, to include a "duck" type as a method parameter type? This would look as follows: void myFunction(duck foo) { foo.doStuff(); } This could be syntactic sugar for invoking doStuff() via reflection, or it could be implemented different...

in python, is there a one line pythonic way to get a list of keys from a dictionary in sorted order?

The list sort method is a modifier function that returns None. So if I want to iterate through all of the keys in a dictionary I cannot do: for k in somedictionary.keys().sort(): dosomething() instead, i must: keys = somedictionary.keys() keys.sort() for k in keys: dosomething() Is there a pretty way to iterate through t...

I want more syntactic sugar in my Java!

Syntactic sugar for properties for example in C#: private int x; public int X{ get { return x; } set { x = value; } } or simply public int X{ get; set; } I am missing verbatim strings in java... @"C:\My Documents\" instead of "C:\\My Documents\\" Do you agree Java needs more "sugar"? Any one knows is there is sugar comin...

if the with keyword is bad (for C#), is this a nice alternative?

var insInvoice = new NpgsqlCommand( @"INSERT INTO invoice_detail( invoice_id, invoice_detail_id, product_id, qty, price, amount) VALUES ( :_invoice_id, :_invoice_detail_id, :_product_id, :_qty, :_price, :_qty * :_price)", c); with(var p = insInvoice.Parameters) { p.Add("_invoice_id", NpgsqlDbType.Uuid, 0, "invoice_id"); ...

Elegant ways to return multiple values from a function

It seems like in most mainstream programming languages, returning multiple values from a function is an extremely awkward thing. The typical solutions are to make either a struct or a plain old data class and return that, or to pass at least some of the parameters by reference or pointer instead of returning them. Using references/poin...

C# property and ref parameter, why no sugar?

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(re...

Why is Syntactic Sugar sometimes considered a bad thing?

Syntactic sugar, IMHO, generally makes programs much more readable and easier to understand than coding from a very minimalistic set of primitives. I don't really see a downside to good, well thought out syntactic sugar. Why do some people basically think that syntactic sugar is at best superfluous and at worst something to be avoided?...

How can I hide all java.lang.Object methods from code completion?

Hello, i want to create some simple wrapper classes for an existing class library. To make the syntax nice to read AND nice to guess (via code completion) i'd like to remove the methods of java.lang.Object. The problem is that all non-atomic things in java inherit from Object and thus have these methods. I already tried to create the wr...

How to mix binding (<-) and assignment (let) in one line? (in Haskell)

This is about syntactic sugar in Haskell. A simple Haskell program: main = do args <- getArgs let first = head args print first I use binding in the first line (args <- getArgs) and a pure assignment in the second one (let first = ...). Is it possible to merge them together into a readable one-liner? I understand that I can rew...

Scala Map: mysterious syntactic sugar?

I have just found out this syntax for a scala Map (used here in mutable form) val m = scala.collection.mutable.Map[String, Int]() m("Hello") = 5 println(m) //PRINTS Map(Hello -> 5) Now I'm not sure whether this is syntactic sugar built in to the language, or whether something more fundamental is going on here involving the fact that a...

2D matrix and overloading operator() / ugly syntax

I'm using a 2D matrix in one of my projects. It's something like it is suggested at C++ FAQ Lite. The neat thing is that you can use it like this: int main() { Matrix m(10,10); m(5,8) = 106.15; std::cout << m(5,8); ... } Now, I have a graph composed of vertices and each vertex has a public (just for simplicity of the example)...

How do I add syntactic sugar in my Javascript library?

Right now the library can translate this operation Select * from List where name = k% order by desc to List.filter(function(x) { return x.first_char() == 'k' }).sort().reverse()); Whats the best hack to remove the () so that the developer can write statements like: List.filter(fn(x) { return x.first_char == 'k' }).sort.reverse; ...

C# - Syntactic sugar for out parameters?

Let us say for a moment that C# allowed multiple return values in the most pure sense, where we would expect to see something like: string sender = message.GetSender(); string receiver = message.GetReceiver(); compacted to: string sender, receiver = message.GetParticipants(); In that case, I do not have to understand the return val...

Is it worth the effort to have a function that returns the inverse of another function?

I have recently added a HasValue function to our internal javascript library: function HasValue(item) { return (item !== undefined && item !== null); } A during a convorsation with a coworker, we came up with the idea of also adding another function that would basically just be the inverse: perhaps HasNoValue, or IsNothing If we ...

Haskell: difference between . (dot) and $ (dollar sign)

Can anybody explain what the difference is in Haskell between the dot (.), and the dollar sign ($). As I understand it, they are both syntactic sugar for not needing to use parentheses. ...

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

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...

Code equivalent to += assignment to an event

I was wondering if anyone could tell me the raw code equivalent to the += operator for adding a method to an event. I am curious to how it it works from a technical standpoint. ...

Python assert -- improved introspection of failure?

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 ...