Live data from Hacker News

Java 8 Lambdas

datumedge.blogspot.co.uk

41–50 of 144 posts

Re: Java 8 Lambdas

#41

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…

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 are lots of terrible programmers out there. The average level of programming skills on HN is considerably higher than you'll find at most companies.

I've seen so much bad code written by people with senior job titles; confusing, ugly, inflexible, but working code.

Re: Java 8 Lambdas

#42
post #36
post #26

Earlier quoted context omitted.

> I don't know what you mean by "sorting key" in this context. He means the item by which you sort. The "by" in your "SortBy" example.

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");

Re: Java 8 Lambdas

#43

Would have been a big deal - if they introduced 10 years ago

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.

There is a difference between required and useful, I can build a house without using a hammer, but that does not mean there useless.

Re: Java 8 Lambdas

#44
post #21

Earlier quoted context omitted.

> It's much better to have comparator functions for the most common types baked in, and use projection to select a list of fields to use to compare with. Except that requires a sorting key for the result. Trivial for dynamically typed languages, but how do you handle the type of that key in a statically typed language? Mandate that the key be a string? Build a limited set of overloads against a dedicated type? And ev…

I don't know what you mean by "sorting key" in this context. The idiom I have in mind is how .Net implements "SortBy", "ThenBy" and friends. Overloads can be used for a comparator function based approach, but it should not be the first choice. And I'll stand by comparators being easy to get wrong, particularly when values may be null, polymorphic, etc.

> I don't know what you mean by "sorting key" in this context.

The result of the projection which is associated with each value and used to actually sort the values.

> The idiom I have in mind is how .Net implements "SortBy", "ThenBy" and friends.

I assume you mean OrderBy. I see, instead of having sorting as a single atomic operation it becomes a stateful multi-step transformation of the iterator. I can still see weirdness in it: what are the constraints on the return value of the key selector functions? The MSDN does not list any, but that can't be right: what if the key selector returns a value which can not be sorted itself?

> And I'll stand by comparators being easy to get wrong, particularly when values may be null, polymorphic, etc.

Trivial key extraction does not make that any easier: instead of being hard, hard cases become impossible.

Re: Java 8 Lambdas

#45
post #30

Earlier quoted context omitted.

I call BS. def return_closure (value): def closure (): return value return closure foo = return_closure(5) print foo() # prints 5 If you look closely, foo is an anonymous function, and it could have anything you bloody well please inside of it. What you can't do is put statements in an anonymous function that you are declaring inline. But the full syntax I just demonstrated is only marginally longer. UPDATE Modify th…

If you look closely, foo is an anonymous function No, "foo" is a variable holding a reference to a named function--the function named "closure". You seem to be confused as to what an anonymous function is. (It doesn't per se have anything to do with closures.) GP is right, Python's lambdas are crippled, unfortunately. And I'm saying that as someone who loves Python.

At that point it is not a named function. The name was only associated with the function in a scope that is now exited.

Call the outer function 3 times. You'll wind up with 3 functions. They will be independent, even though you think they are all named the same thing.

There is literally nothing you could want to do with anonymous functions that you cannot do with this technique. (Of course if you want to update an enclosed variable, you have to store it in a mutable data structure. But that is true for any Python function that wants to mutate data in its surrounding environment.)

Re: Java 8 Lambdas

#46
post #5

I don't know how I feel about interface defaults. Why is that more important than say declared properties or unsigned types?

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…

alright that makes sense. it's good they are worrying about that.

The required exception handling can go next because it doesn't make sense if you version and add new exceptions. Your complier required exception handling was just thrown out.

Re: Java 8 Lambdas

#47
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).

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.

Re: Java 8 Lambdas

#48
post #45

Earlier quoted context omitted.

If you look closely, foo is an anonymous function No, "foo" is a variable holding a reference to a named function--the function named "closure". You seem to be confused as to what an anonymous function is. (It doesn't per se have anything to do with closures.) GP is right, Python's lambdas are crippled, unfortunately. And I'm saying that as someone who loves Python.

At that point it is not a named function. The name was only associated with the function in a scope that is now exited. Call the outer function 3 times. You'll wind up with 3 functions. They will be independent, even though you think they are all named the same thing. There is literally nothing you could want to do with anonymous functions that you cannot do with this technique. (Of course if you want to update an en…

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)

Re: Java 8 Lambdas

#49
post #45

Earlier quoted context omitted.

At that point it is not a named function. The name was only associated with the function in a scope that is now exited. Call the outer function 3 times. You'll wind up with 3 functions. They will be independent, even though you think they are all named the same thing. There is literally nothing you could want to do with anonymous functions that you cannot do with this technique. (Of course if you want to update an en…

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.

Re: Java 8 Lambdas

#50
post #41

Earlier quoted context omitted.

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…

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.

Post reply on HN