by Avalaxy · 14y ago · 0 comments
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).
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.
It is a pain to write if you don't like having to let the IDE do everything for you, though.
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.)C# lambda expression syntax (the =>) are about 4 years old now.
However going the other direction is really easy and straight forward. IKVM.NET has been running Java on top of .NET for years.