4 ms·
Care to elaborate? Ruby had them from day 1, PHP has only had them since 5.3, not sure what the deal is with python.
by alinajaf 14y ago
Care 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.)
- julian37 14y ago
- 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.