Live data from Hacker News

Java 8 Lambdas

datumedge.blogspot.co.uk

51–60 of 144 posts

Re: Java 8 Lambdas

#51

Earlier quoted context omitted.

C# has had them for a little while now.

OT, but all this language comparison got me wondering: has anyone implemented C# on the JVM? :-)

yes, Mainsoft Grasshopper. But given that the CLR is superset of JVM features, you can't easily convert .NET MSIL to Javabyte code (a few research projects tried but it's not pratical). Grasshopper did it at a language level and wrote a C# to Java bytecode compiler.

However going the other direction is really easy and straight forward. IKVM.NET has been running Java on top of .NET for years.

Re: Java 8 Lambdas

#52

Earlier quoted context omitted.

And what difference has the lack of lambda's really made? I cant stand these "my language is better than your language" debates. Use whatever one makes sense for the job (or, in the case of the enterprise, you can hire enough of).

The difference is readability and developer efficiency. the non-lambda version in the example is four times the size of the lambda version. I don't know about you, but I can only fit about thirty lines of source code in my brain at once. Virtually every other modern programming language has a lambda, it's good to see Java catch up. This isn't about Java versus any particular language.

When it comes to how much code you can keep in your head, do you really keep the boilerplate there? Whenever I've used Java and had to attach an ActionListener or whatever, I don't think of it as being an anonymous class with a single method, I think of it as just the logic in the method when I'm reasoning about the program.

It is a pain to write if you don't like having to let the IDE do everything for you, though.

Re: Java 8 Lambdas

#53
post #2

Congratulations to the Java-programmers for getting a feature that virtually every other OOP language has had for about 10 years.

And what difference has the lack of lambda's really made? I cant stand these "my language is better than your language" debates. Use whatever one makes sense for the job (or, in the case of the enterprise, you can hire enough of).

Do you mean to say that one language can't be objectively better than another?

Re: Java 8 Lambdas

#54
post #39

Earlier quoted context omitted.

I think you are mistaken. For 99%+ of business level code which wires up pre-built J2EE components and builds domain models, then you simply don't need them. Even in C# for example, I've seen huge systems written in .Net 4.0 without a single lambda being required.

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

Re: Java 8 Lambdas

#55

Earlier quoted context omitted.

They can't _really_ infer the type. You just have to have one less set of pizzas. It doesn't work like scala's type inference.

It really does infer the type of the lambda. See Part F of the specification: http://download.oracle.com/otndocs/jcp/lambda-0_5_1-edr2-spe...

ah yes I see what you mean. My bad.

Re: Java 8 Lambdas

#56
post #49

Earlier quoted context omitted.

You are confusing closures and anonymous functions. The two are somewhat related, but not the same thing. http://en.wikipedia.org/wiki/Anonymous_function http://en.wikipedia.org/wiki/Closure_(computer_science)

There is no confusion here. Once the function is passed out of the scope that it was created in, it becomes anonymous. If you disagree, demonstrate how it is at that point associated with any available identifier.

This is getting a bit tiresome, but I'll play along since your high karma leads me to believe you're genuinely confused and not a troll.

The point of anonymous functions is not whether or not they will eventually be bound to an identifier, i.e. what happens with the function later on. The point is terseness in declaration: the ability to avoid having to define a full-blown function for small pieces of code that you want to pass to a higher-order function.

In Python, the difference between:

   def is_not_zero(x): return x != 0
   foo = bar.filter(is_not_zero)
and

   foo = bar.filter(lambda x: x != 0)
The Python language construct for anonymous functions is "lambda". Nothing more, nothing less. Closures don't have anything to do with it, except that of course both named functions and lambdas have closures created when they are referenced.

This is my last contribution to this thread. I'm just trying to help clear up an apparent misunderstanding on your part.

Re: Java 8 Lambdas

#57
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 Haskell, sortBy takes a comparison function:

  sortBy :: (a -> a -> Ordering) -> [a] -> [a]
And Ordering is defined as:

  data Ordering = LT | EQ | GT
If you want to provide a "key" function rather than a comparison function, you can use sortOn:

  sortOn :: Ord b => (a -> b) -> [a] -> [a]
Which is basically just a sortBy with a function that applies the key function and then compares as normal.

Re: Java 8 Lambdas

#58
post #24
post #2

Congratulations to the Java-programmers for getting a feature that virtually every other OOP language has had for about 10 years.

The feature has been in Java for a long time. This is just nice syntax for anonymous inner classes.

The feature has not been in Java for a long time, because the feature is the nice syntax.

Re: Java 8 Lambdas

#59
post #4
post #2

Congratulations to the Java-programmers for getting a feature that virtually every other OOP language has had for about 10 years.

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.

Re: Java 8 Lambdas

#60
post #50
post #41

Earlier quoted context omitted.

I can give many examples where I've handed code over and later there was problems due to less-skilled developers. One good example I have is being called by a client who had a bug they couldn't fix in some code I had written years before. After looking into it, I found something like this: // ABC 1/1/2001 removed this call as we don't understand why it was being called // x = performImportantCalculationABC(); There a…

You're forgetting that the smarter programmers realise all they need to do is fix something really fast, so their managers are like, "oh my goodness, you're excellent!" but if you get caught up on the details, spend several weeks "fixing" something that your manager won't even see or understand, you're a shit programmer. So really, you gotta get your priorities straight.

If your priority is "ingratiate yourself to your manager" then yeah, it's best to pick small superficial problems or prefer patch-job fixes. These decisions tend to be informed by how much technical debt the "short" fix makes.

But if a problem genuinely will take 6 weeks to solve, then it's probably a worthwhile problem to solve. Those sorts of problems tend to have far-reaching implications for sites and their ability to scale. The big question is when to solve it. Competent engineers know when to do this.

Managers who can't see this? Bad managers. Bad managers can kill a startup just as effectively as 'rockstar' engineers.

Post reply on HN