syntactic-sugar

Python assert — improved introspection of failure?

余生颓废 提交于 2019-11-28 23:35:09
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 encoded in the expression that is being asserted. DRY (Don't Repeat Yourself). Install your of function as

Elegant ways to return multiple values from a function

会有一股神秘感。 提交于 2019-11-28 22:43:41
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/pointers is pretty awkward because it relies on side effects and means you have yet another parameter to pass. The class/struct solution is also IMHO pretty awkward because you then end up with a million little classes/structs that are only used to return values from

Java in operator

无人久伴 提交于 2019-11-28 20:11:54
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 similar types: switch (value) { case a: case b: case c: // .. break; case d: case e: // .. break; } Or

Is there a literal notation for an array of symbols?

大兔子大兔子 提交于 2019-11-28 16:15:58
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. David Grayson 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. This feature was originally announced here: http://www.ruby-lang.org/zh_TW/news/2012/11/02

Is foreach purely “syntactic sugar”?

北城以北 提交于 2019-11-28 10:57:47
The compiler compiles a foreach loop into something like a for loop when the foreach is used with an array. And the compiler compiles a foreach loop into something like a while loop when the foreach is used with an IEnumerable or IEnumerable<T> . So does this mean foreach is purely syntactic sugar ? Or is there anything sophisticated about it? Does the CLR know about foreach ? Is there anything specifically designed for foreach in the MSIL code? It's purely syntactic sugar in that you could obtain the same behaviour without it, yes. Many other things are the same... for , while etc... To

Objective-C: message syntax vs. dot syntax; what's the difference?

穿精又带淫゛_ 提交于 2019-11-28 10:30:40
If I'm using @synthesize foo; , what's the difference between the following: // message syntax [myObj setFoo:5]; [myObj foo]; and // dot syntax myObj.foo = 5; myObj.foo; I like the consistency of the dot syntax but I don't know if it's doing something I should be I concerned about. Any additional information would be a great help. There is no functional difference between using dot syntax and using message syntax. I find that using message syntax is more consistent with the language as a whole and that dot syntax was just implemented as a convenience to programmers who were coming over from

Closing over the Loop Variable in C#

非 Y 不嫁゛ 提交于 2019-11-28 10:19:26
From this post, I was told that the following section of code suffered from "the egregious act of closing over the loop variable." foreach (Canidate canidate in allCanidates) { Thread newThread = new Thread(delegate() { BusyWait(canidate); }); newThread.Start(); } I switched it to this: foreach (Canidate canidate in allCanidates) { var can = canidate; Thread newThread = new Thread(delegate() { BusyWait(can); }); newThread.Start(); } But my boss keeps insisting that it will suffer from the same issues. I used this link to try to solve this issue. Can someone help me to correctly solve the issue

How useful is C#'s ?? operator?

别说谁变了你拦得住时间么 提交于 2019-11-28 04:59:42
So I have been intrigued by the ?? operator, but have still been unable to use it. I usually think about it when I am doing something like: var x = (someObject as someType).someMember; If someObject is valid and someMember is null, I could do var x = (someObject as someType).someMember ?? defaultValue; but almost invariably I get into problems when someObject is null, and ?? doesn't help me make this any cleaner than doing the null check myself. What uses have you guys found for ?? in practical situations? I agree with you that the ?? operator is usually of limited use-- it's useful to provide

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

前提是你 提交于 2019-11-28 04:28:11
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 extension on Array with parameters (array instance, all elements function, first elements function, last

Python decorator best practice, using a class vs a function

匆匆过客 提交于 2019-11-28 03:02:05
As I've understood it there are two ways to do a Python decorator, to either use the __call__ of a class or to define and call a function as the decorator. What's the advantages/disadvantages of these methods? Is there one preferred method? Example 1 class dec1(object): def __init__(self, f): self.f = f def __call__(self): print "Decorating", self.f.__name__ self.f() @dec1 def func1(): print "inside func1()" func1() # Decorating func1 # inside func1() Example 2 def dec2(f): def new_f(): print "Decorating", f.__name__ f() return new_f @dec2 def func2(): print "inside func2()" func2() #