Live data from Hacker News

Java 8 Lambdas

datumedge.blogspot.co.uk

61–70 of 144 posts

Re: Java 8 Lambdas

#61
post #25

Earlier quoted context omitted.

> Java is still widely used. I'm looking forward to this -- there are some projects we can't take to Scala due to client concerns around a less mainstream language. I hope I am wrong but I smell a trap here. Some time ago I implemented a solution (for .Net, that is) that employed some more modern techniques ( reflection, recursive lambdas, etc). Unfortunately many of the programmers in charge of maintaining my code w…

Perhaps they didn't approve of your use of reflection? Your post sounds a bit like you would use it because you can create awesome looking code. It should be used rather seldom http://stackoverflow.com/questions/429962/when-do-you-use-re...

Are you kidding? Reflection is one of the most powerful parts of the .NET framework. Hell, the page you link to contains many examples of the different possibilities, but they're just a start. The only knock I know of against reflection is that it's a performance hit. If that's really the issue, write the code using reflection (it's going to be better code, most likely), and then see if performance is an issue. If it is, refactor out the reflection.

Reflection makes things possible that simply aren't possible without it. It can get rid of layers upon layers of complexity. It can shrink thousands of lines of code to fractions of that.

There is no reason to say that 'it should be used seldom.' It's a powerful tool and should be wielded along with every other tool in the toolbox.

Re: Java 8 Lambdas

#62
post #42
post #36

Earlier quoted context omitted.

Yes, but that reading doesn't make sense given what he also wrote - that apparently this would make things more awkward in a static language.

How do you define the sorting key in a static language? With reflection? e.g if anArray is a Java array of Employee objects, and you want to sort on their salary attribute, how do you specify it? anArray.sortBy(???); In a dynamic language it doesn't matter much, since all attribute access amounts to (and is as slow as) reflection on a static language anyway, so you can do something like, say: anArray.sortBy("salary")…

In JDK8 there are method references that could enable this. For example, you can reference a method in a class like Person::age ( equivalent to (Person person) -> person.age() ) that could then be used to sort the collection since Integer implements Comparable.

Re: Java 8 Lambdas

#63
post #39

Earlier quoted context omitted.

In your sense, you don't need them for 100% of the code. Lambdas are NEVER required, as tons of other syntactic features are never required. They are nice to have though, because they make the code more concise and reduce bugs but reducing boilerplate and repetition. Working without them when you have them at your disposal is idiotic. That huge .Net 4.0 systems have been written without a single lambda probably speak…

I don't think that they reduce bugs at all - that's a bit of a wired assertion to make. Its not about the programmers but the architecture. To be honest, the primary use case for lambda expressions in .net is LINQ and possibly container configuration. If you use NHibernate and do not expose IEnumerable anywhere (which is required across network boundaries and encapsulation boundaries) then you just don't see it anywh…

I think the implication here is that if they require fewer lines of code, they are going to reduce the number of bugs.

Regarding LINQ: is there any .NET app out there that isn't using IEnumerable at some point? I can't recall every writing an app that didn't use it, unless it was an extremely simple app. Besides that, Lambdas are very useful in event subscription.

Re: Java 8 Lambdas

#64
post #42
post #36

Earlier quoted context omitted.

Yes, but that reading doesn't make sense given what he also wrote - that apparently this would make things more awkward in a static language.

How do you define the sorting key in a static language? With reflection? e.g if anArray is a Java array of Employee objects, and you want to sort on their salary attribute, how do you specify it? anArray.sortBy(???); In a dynamic language it doesn't matter much, since all attribute access amounts to (and is as slow as) reflection on a static language anyway, so you can do something like, say: anArray.sortBy("salary")…

AnArray.Sort(e -> e.Salary) and if there's no built-in way to compare two Salary objects (let's say they're complex objects), then you come up with an interface that the sorting function would use and the objects would implement. This way you let the objects compare themselves.

Re: Java 8 Lambdas

#65

Earlier quoted context omitted.

> Java is still widely used. I'm looking forward to this -- there are some projects we can't take to Scala due to client concerns around a less mainstream language. I hope I am wrong but I smell a trap here. Some time ago I implemented a solution (for .Net, that is) that employed some more modern techniques ( reflection, recursive lambdas, etc). Unfortunately many of the programmers in charge of maintaining my code w…

I smell trolling, however considering this anecdote is genuine: (1) interns should never be allowed to commit code in the master branch without a code review from a senior developer (2) there are many competent developers in India, or around the world, so if your company needs to outsource, it can do so by being a little careful about who they employ (3) if the above 2 are not possible, simply look for another job, b…

(1) No one be allowed to commit code in the master branch without a code review from another developer

FTFY

Re: Java 8 Lambdas

#66

Earlier quoted context omitted.

They are hugely important for introducing new behavior on existing interfaces without breaking compability. They are at the core of what is going to allow you to use all the new functional-style collection methods on your old collections without changing your implementation code at all. Declared properties and unsigned types are relatively small changes. BTW, in JDK8 they have provided limited support for unsigned ty…

Agreed and it's an order of magnitude better than extension methods which .Net uses for the same functionality!

C# extension methods are used for considerably more than adding new methods to existing interfaces, though that is a valid use of them.

They also don't require you have access to the source (anybody can write an extension method on System.Collections.IEnumerable, not just the BCL team). That they work on any type (not just interfaces) also makes them considerably more general than default implementations.

I also find the idea of an interface carrying implementation a little weird, the abstract class/interface line gets a lot blurrier.

That said, lambda support that only worked on new code would be pretty lackluster; so it's good some thought has gone into getting them into legacy code.

Re: Java 8 Lambdas

#67
post #61
post #25

Earlier quoted context omitted.

Perhaps they didn't approve of your use of reflection? Your post sounds a bit like you would use it because you can create awesome looking code. It should be used rather seldom http://stackoverflow.com/questions/429962/when-do-you-use-re...

Are you kidding? Reflection is one of the most powerful parts of the .NET framework. Hell, the page you link to contains many examples of the different possibilities, but they're just a start. The only knock I know of against reflection is that it's a performance hit. If that's really the issue, write the code using reflection (it's going to be better code, most likely), and then see if performance is an issue. If it…

It seems to me that many well-known "best" practices need to be considered with more context.

In an environment with a large, frequently changing team, heavy use of features that provide extra abstraction may result in less-skilled team members being confused. Most enterprise environments consider that normal. Most startups would fire a programmer who couldn't grasp reflection.

Re: Java 8 Lambdas

#68
post #61
post #25

Earlier quoted context omitted.

Perhaps they didn't approve of your use of reflection? Your post sounds a bit like you would use it because you can create awesome looking code. It should be used rather seldom http://stackoverflow.com/questions/429962/when-do-you-use-re...

Are you kidding? Reflection is one of the most powerful parts of the .NET framework. Hell, the page you link to contains many examples of the different possibilities, but they're just a start. The only knock I know of against reflection is that it's a performance hit. If that's really the issue, write the code using reflection (it's going to be better code, most likely), and then see if performance is an issue. If it…

Yeah, I don't know about that. When I first discovered reflection, I was using it for everything. Initially it was awesome but then I've quickly realised that it's exactly the kind of thing that makes code a lot more complicated than it should be, especially with the deadly reflection + expression trees (runtime code generation) combo. It's really a dark path that you should stay away from. Basically, unless your problem involves having to get metadata about .NET assemblies, reflection is not the right solution. Code that's clever for the sake of cleverness is cool and entertaining but has no place in production.

Re: Java 8 Lambdas

#69
The idea of 'default methods' looks quite disturbing. It seems to allow implementation of some methods inside interfaces. Which is against the basic idea of interface, isn't it? Correct me if I'm wrong, but multiple inheritance problems form C++ are now going to be a brand new feature in Java.

Re: Java 8 Lambdas

#70
post #59
post #4

Earlier quoted context omitted.

Care to elaborate? Ruby had them from day 1, PHP has only had them since 5.3, not sure what the deal is with python.

Didn't Ruby have dynamic scoping in day 1? Dynamically scoped lambdas are different lambdas, and much less useful.

If I'm understanding you correctly, I believe it has both. Blocks act as closures and lambdas are dynamically scoped, though you can convert between the two and change the scoping in either case.
Post reply on HN