Live data from Hacker News

Java 8 Lambdas

datumedge.blogspot.co.uk

101–110 of 144 posts

Re: Java 8 Lambdas

#101
post #21

Earlier quoted context omitted.

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 we…

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

Perhaps you missed this bit:

> > Overloads can be used for a comparator function based approach, but it should not be the first choice.

Key extraction makes trivial cases less error prone than comparators; overloads can be used to make the hard cases possible. Comparators everywhere make all cases error-prone.

Re: Java 8 Lambdas

#102
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.

You can unbind it by deleting it after using it so it is practically anonymous.

Re: Java 8 Lambdas

#103

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…

No, I was not trolling. The key to understand the whole thing is that it was not a software company, but a construction company and I was there as a consultant. For them, software is a cost, not a source of revenue. That's why they didn't implement rigorous practices such as code reviews. That's why they'd prefer the cheaper against the better coders. That's why I already followed your 3rd advice.

It's hard for me to think of a good reason why a construction company should be writing custom software, instead of buying something off the shelf.

Re: Java 8 Lambdas

#104

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.

> 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…

Funny, I've had it the other way: I was working on a project as a senior dev and had interns writing Godless functional code using Guava and Functional Java, and having a grand old time seeing how much concurrent code they could fit into a single statement.

The fact is that Java is not a functional language and to bolt on functional pieces and then ignore the imperative, OO nature of Java is stupid. Don't get me wrong, I love functional programming, but Java is not and will never be a functional programming language, and developers that hate on programmers who use a language in the manner in which it was intended to be used are the real fools.

It is a poor programmer indeed that writes pathological code (and functional code in Java is pathological) and then blames his team for not understanding it.

Re: Java 8 Lambdas

#105
post #92
post #79

Earlier quoted context omitted.

it's a closure, but it's not an anonymous function; it has a name! "anonymous function" is a syntactic feature but "lexical closure" is a language feature. In python "anonymous functions" can comprise only one expression: `lambda x: x * 2`, but that's not the only way to make a lexical closure, you can also do: def foo(x): return x * 2 and then use `foo` in place of the anonymous function literal.

What name does it have AFTER it has been returned from the environment that it was created in? If you have 5 different things that are all different but supposedly have the same name, what does it mean to have a name?

"anonymous function" is a term that refers to a syntax for declaring a function without immediately naming it. A function can't "become" anonymous at runtime by leaving the environment in which it was created.

"lexical closure" is a term used to describe the way that functions behave at runtime. A lexical closure can be used as a first class value, and retains the scope that it was created in, as opposed to adopting the scope in which is is called. Example:

    def foo(bar):
        a = 2
        bar()
    
    a = 5
    def baz():
        print a
will print "5" not "2". but note that `baz` is not anonymous because being anonymous is a syntactic quality and in the source code, `baz` is named in the same operation as it is declared.

Re: Java 8 Lambdas

#106

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…

Funny, I've had it the other way: I was working on a project as a senior dev and had interns writing Godless functional code using Guava and Functional Java, and having a grand old time seeing how much concurrent code they could fit into a single statement. The fact is that Java is not a functional language and to bolt on functional pieces and then ignore the imperative, OO nature of Java is stupid. Don't get me wron…

There are significant lessons to be learned from functional programming, the least of which is the value of immutability in making complex systems comprehensible.

Immutability is almost completely necessary for easy-to-understand concurrency, and extremely helpful in designing easy-to-understand APIs and operations on in-memory-models.

It is a poor programmer indeed that discards the lessons of FP and instead writes purely imperative OO. Your interns may have been writing bad code, but it wasn't the "functional" part that was the problem.

Re: Java 8 Lambdas

#107

Earlier quoted context omitted.

> To be honest, the primary use case for lambda expressions in .net is LINQ Uhm, yes, if you're incapable of using delegates and writing functions that take delegates as parameters, then you'll only use lambda expressions where other people have written such functions, for example in LINQ or the generic collections in the framework. But if you can use delegates, and if you can use lambda expressions, they're a wonder…

If you're considering Action and the numerous Func implementations, then I disagree mostly. They decrease efferent coupling but do not decrease bugs. There is no direct correlation between readability and bugs from my experience. Some of the least buggy code I've seen is messy and likewise the most buggy can be the most readable. There is not enough correlation to draw that conclusion. The metric that is important is…

I have one for your list:

0. APIs that don't leverage the compiler to enforce correctness and catch bugs at compile time.

Designing APIs in such a way often requires FP language features. You can often model equivalent APIs without FP features, but they'll be so verbose as to be unusable.

Re: Java 8 Lambdas

#108
post #90

Earlier quoted context omitted.

I don't see any pitfalls that wouldn't arise with imperative techniques. Well, Single() throws if there is no or more than one item with the property name "Blah!" in the collection, but if you just want the first one or none you should use FirstOrDefault() anyway. The point is that you need to write roughly a dozen lines with more room for hand grenades to reproduce that functionality with imperative constructs: Thin…

You miss one important point, which I've learned from many years of using .Net: Which dereference in your LINQ expression is causing the NullReferenceException? Try solving that problem when it goes pop in production and you have a couple of million quid in flight! I'd write it as follows (probably wrapped as a generic function of T: public Thing GetItemWithName(ICollection collection) { Thing output; int found; // p…

Well, if the error reporting of Enumerable.Single() isn't good enough for you (i think it throws InvalidOperationExceptions with different messages for both error cases), nothing stops you from implementing it once for yourself and profit from the benefits everytime instead of writing the same looping constructs interlaced with error reporting again and again.

    public static T MySingle(
                this IEnumerable collection, 
                Func pred,
                string foundNoneMsg,
                string notUniqueMsg) {
        Check.IsNotNull(collection, "collection was null");
        var filtered = items.Where(item => pred(item));
        Check.IsTrue(filtered.Any(), foundNoneMsg);
        Check.IsFalse(filtered.Skip(1).Any(), notUniqueMsg);
        return filtered.Single();
    }

    bool HasName(this Thing item, string name) {
         Check.IsNotNull(item, "item was null");
         Check.IsNotNull(item.Property, "item.Property was null");
         Check.IsNotNullOrWhiteSpace(item.Property.Name, "item.Property.Name was null");
         Check.IsNotNullOrWhiteSpace(name, "name was null or whitespace");
         return item.Property.Name
                .CompareTo(name, StringComparison.CultureInvariantIgnoreCase);
    }

    collection.MySingle(
            item => item.HasName("Blah!"),
            foundNoneMsg: "collection has no item with Property.Name 'Blah!'
            notUniqueMsg: "collection contained more than one 'Blah!');

Re: Java 8 Lambdas

#109
post #99
post #96

I was recently at a Java conference where Juergen Hoeller (of Spring fame) was a keynote speaker. In one of his talks he said something like "Once again, Java is solving yesterday's problems". The moment he said that, I thought to myself "Yes! So true!". The day before, Angelika Langer gave a talk on Lambdas in Java 8. I watched as the other developers listened with half-opened mouths (and some of them, with confused…

I don't understand or agree with your logic at all. Oracle shouldn't waste time on Java because it is already so far behind as to be hopeless? That notion seems silly at best, especially as 99% of code written on the JVM is still done in Java. Also, generics fiasco? Generics may not be perfect, but I haven't met a single person who isn't glad that they are in the language. Who has done generics better? C#? Because C#…

As a "language snob", I feel that Java is hopelessly behind among JVM languages. But I also think that C# is well-designed enough that it doesn't matter whether it's the only CLR language I'll ever use. I don't know if the grandparent poster feels the same, but your post actually confirms my opinion.

Of course I have no realistic idea how Oracle would ever migrate millions of Java coders to a cleaner language. :(

Re: Java 8 Lambdas

#110
I do not understand how this really any different that what we already have with anonymous classes/interfaces. If we have to continue to define interfaces for our lambdas then we might as well continue to use anonymous classes and remove the final restriction (that's completely backward compatible). The final restriction wasn't required in the original design of anonymous classes before the community freaked out over memory allocation of primitives. The reason closures are a pain in Java is having to meet the static typing declarations using interfaces for our anonymous classes. The whole point to this was to create a compact syntax for declaring methods that take lamdas as parameters without resorting to interfaces.

I also wish that default mechanism could be extended to existing classes as well so we could add methods to classes because the real question is what are the API changes to collection, File, etc with respect to the addition of lambdas. The places where Java has fallen short in the past is the API (Collection.join? Collection.map, Collection.reduce, new File( String... paths), etc).

Post reply on HN