When to use lambda, when to use Proc.new?

Another important but subtle difference between procs created with lambda and procs created with Proc.new is how they handle the return statement: In a lambda-created proc, the return statement returns only from the proc itself In a Proc.new-created proc, the return statement is a little more surprising: it returns control not just from the proc, … Read more

How can I throw CHECKED exceptions from inside Java 8 streams?

The simple answer to your question is: You can’t, at least not directly. And it’s not your fault. Oracle messed it up. They cling on the concept of checked exceptions, but inconsistently forgot to take care of checked exceptions when designing the functional interfaces, streams, lambda etc. That’s all grist to the mill of experts … Read more

Break or return from Java 8 stream forEach?

If you need this, you shouldn’t use forEach, but one of the other methods available on streams; which one, depends on what your goal is. For example, if the goal of this loop is to find the first element which matches some predicate: Optional<SomeObject> result = someObjects.stream().filter(obj -> some_condition_met).findFirst(); (Note: This will not iterate the … Read more

Reflecting parameter name: abuse of C# lambda expressions or syntax brilliance? [closed]

I find that odd not so much because of the name, but because the lambda is unnecessary; it could use an anonymous-type and be more flexible: .Attributes(new { style = “width:100%”, @class=”foo”, blip=123 }); This is a pattern used in much of ASP.NET MVC (for example), and has other uses (a caveat, note also Ayende’s … Read more

No Multiline Lambda in Python: Why not?

Guido van Rossum (the inventor of Python) answers this exact question himself in an old blog post. Basically, he admits that it’s theoretically possible, but that any proposed solution would be un-Pythonic: “But the complexity of any proposed solution for this puzzle is immense, to me: it requires the parser (or more precisely, the lexer) … Read more

When should I use arrow functions in ECMAScript 6?

A while ago our team migrated all its code (a mid-sized AngularJS app) to JavaScript compiled using Traceur Babel. I’m now using the following rule of thumb for functions in ES6 and beyond: Use function in the global scope and for Object.prototype properties. Use class for object constructors. Use => everywhere else. Why use arrow … Read more