6 ms·
Congratulations to the Java-programmers for getting a feature that virtually every other OOP language has had for about 10 years.
by Avalaxy 14y ago
Congratulations to the Java-programmers for getting a feature that virtually every other OOP language has had for about 10 years.
- deleted 14y ago[deleted]
- alinajaf 14y agoCare to elaborate? Ruby had them from day 1, PHP has only had them since 5.3, not sure what the deal is with python.
- X6MW3aQrZU5VKkz 14y agoC# has had them for a little while now.
- kmontrose 14y agoC# has had a notion of delegates since day one, and anonymous methods (like delegate(string foo) { ... } ) for 7-ish years. C# lambda expression syntax (the =>) are about 4 years old now.
- Estragon 14y agoOT, but all this language comparison got me wondering: has anyone implemented C# on the JVM? :-)
- zbowling 14y agoyes, 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.
- Xion 14y agoPython has lambdas and functions as first-order values since the beginning.
- krunaldo 14y agoYou can't have statements in pythons gimped versions of anon functions :/
- btilly 14y agoI 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 the end like this to see that the functions really are anonymous. foo = return_closure(1) bar = return_closure(2) baz = return_closure(3) print foo() print bar() print baz() You will see that the three functions returned are completely independent. They are not associated with any name or each other. They are, in fact, anonymous. (Admittedly at the moment they are called foo, bar and baz. But you could have put them into an array, and they really would have no unique name.)
- julian37 14y agoIf 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.
- btilly 14y agoAt 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.)
- yati 14y agoC++, too has fine support for functional programming(since C++11)
- Peaker 14y agoDidn't Ruby have dynamic scoping in day 1? Dynamically scoped lambdas are different lambdas, and much less useful.
- alinajaf 14y agoIf 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.
- brown9-2 14y agoAnd? Everyone agrees it would have been nice to have this a long time ago. It's still nice to get these features added now rather than never.
- qznc 14y agoThe feature has been in Java for a long time. This is just nice syntax for anonymous inner classes.
- spullara 14y agoThough the current implementation does use anonymous inner classes under the covers the intent was to specify them such that other implementations that are more efficient could be used. The likely final implementation will be simple methods invoked with invokedynamic that don't need a separate class and instance per call site.
- Peaker 14y agoThe feature has not been in Java for a long time, because the feature is the nice syntax.
- batista 14y agoYes, and C doesn't have most things other languages have. That doesn't mean I will use Ruby for my next real time embedded project.
- skippy81 14y agoAnd 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).
- bickfordb 14y agoThe 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.
- majormajor 14y agoWhen 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.
- alinajaf 14y agoDo you mean to say that one language can't be objectively better than another?