Live data from Hacker News

Java 8 Lambdas

datumedge.blogspot.co.uk

91–100 of 144 posts

Re: Java 8 Lambdas

#91
post #49

Earlier quoted context omitted.

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

I hate pointless pedantry which is all that this discussion is.

Anyways anonymous function syntax is not always terse. So if terseness is the point, then it is a pointless distinction. Consider JavaScript. I am sure that you'd agree that the following is an anonymous function (Wikipedia certainly agrees that it is):

  function (x, y) {
    return x + y;
  }
But that is not very terse, is it? Even declaring a named function in Haskell is more terse than that by far. (And that anonymous function could just be: \x y -> x + y) But it is unambiguously an anonymous function syntax.

Anyways from the point of view of a programmer, what matters most is what you can say, and then secondly how convenient it is to say it. Anything that you would want to say with anonymous functions, you can say in Python. It will be more verbose by far than a real functional language like Haskell would be. But not really worse than many other scripting languages, like JavaScript.

Re: Java 8 Lambdas

#92
post #79
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…

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?

Re: Java 8 Lambdas

#93

Earlier quoted context omitted.

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…

> 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 the skill of the programmer who fulfilled the specification and their ability to understand the task fully and their ability to translate that understanding to the language at hand and know where and when things will go snap.

Things that DO increase bugs:

1. Crap programmers.

2. Coupling - log increase in side effects of a change.

3. Bad test coverage.

4. Poor design up front.

5. The killer: bad specifications (not even a code issue!).

Using more advanced language features does not necessarily improve things.

Re: Java 8 Lambdas

#94
post #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.

I agree with you. There is a reason why classes in Java can only inherit implementations of methods from a single parent class. If we're going down this road, why not just allow multiple inheritance?

Re: Java 8 Lambdas

#95
post #90

Earlier quoted context omitted.

Not necessarily. collection.Where(s => s.Property.Name == "Blah!").Single(); 4 hand grenades waiting to go off in that expression. Try and spot them. We have a 670kloc platform that doesn't have a single one in it (it's still .Net 2.0). 450 domain objects, 1000+ NH criteria queries and about 650 aspx pages...

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;

        // preconditions
        Check.IsNotNull(collection, "collection was null");

        foreach(var item in collection)
        {
            // ref checks
            Check.IsNotNull(item, "item was null");
            Check.IsNotNull(item.Property, "item.Property was null");
            Check.IsNotNullOrWhiteSpace(item.Property.Name, "item.Property.Name was null or whitespace");
      
            if (string.Compare(item.Property.Name, "Blah!", StringComparison.InvariantCultureIgnoreCase) != 0)
                continue;
            
            found++;

            // rule check
            Check.IsTrue(found 
Note: I tend to write proper industrial grade stuff that has to work every time without fail or any edge cases or conditions. These conditions are prescribed up front. Zero bugs and fail early is the only acceptable outcome which is why this is verbose.

Re: Java 8 Lambdas

#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 expressions on their faces) and wondered why, instead of bolting features other languages have had for decades onto Java (potentially leading to another fiasco like generics), Oracle doesn't instead continue to work on the JVM to give an even better support for languages that already do have these concepts (by, for example, providing support for TCO).

Re: Java 8 Lambdas

#97
post #91

Earlier quoted context omitted.

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

I hate pointless pedantry which is all that this discussion is. Anyways anonymous function syntax is not always terse. So if terseness is the point, then it is a pointless distinction. Consider JavaScript. I am sure that you'd agree that the following is an anonymous function (Wikipedia certainly agrees that it is): function (x, y) { return x + y; } But that is not very terse, is it? Even declaring a named function i…

But it's definitely worse than a scripting language like ruby.

Also, by this definition, the OP is just adding terseness to Java, not lambdas or anonymous functions.

Re: Java 8 Lambdas

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

Because in environments where Java is used "switch language your're using" just isn't such a viable option. Especially in the light of an option of just upgrading the language. There's nothing seriously wrong with Java that couldn't be fixed by incremental language updates - like the ones C# has received in last couple of versions.

Re: Java 8 Lambdas

#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#'s type system is baked so heavily into the run time, the CLR actually has a harder time supporting languages with more powerful type systems than does the JVM.

See, for instance, this post: http://olabini.com/blog/2010/07/questioning-the-reality-of-g.... Both Ola, a JRuby developer, and Martin Oderskey, the creatir of Scala, prefer the JVM's type erasure to the reified generics of the CLR.

Re: Java 8 Lambdas

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

Sometimes I find it a littl bit interesting that .NET community typically lag behind Java community in general (be careful interpreting general vs all).

In Java, the best known book that can be considered comparable with Jon Skeet book is written by Josh Bloch of SUN, Google, and API design fame. He literally suggested to avoid Reflection and use other features unless you are in the business of writing IDE, compiler, code analysis, etc.

I hvae done Reflection in the past and fow not to do that stupid move again and will spend more time figuring other solution. It is not about I cannot grasp Reflection but the code tend to be less elegant, less readable, and not compiled safe.

I would use dynamic languages if I found myself using Reflection a lot.

Post reply on HN