Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Which ones?

Python isn't an exception among languages that people in industry actually use.

For "production grade" server software, which is driving the newfound interest in concurrency, the following languages cover 99% of code written in the last 20 years: C/C++/Java/PHP/Perl/Python/Ruby and the Microsoft Stack (C#, VB).

None of them have anonymous closures. (C# might, but it's also newer, and it's not the the prevailing style of concurrency in any case.)

This is basically JavaScript's Scheme heritage showing through. Anonymous closures are old hat for people who went through a CS program teaching Lisp, but they're not by any means standard in industry.

One of the main reasons that none of the languages needed this feature is because the predominant paradigm for concurrency for the last 2 decades was threading, not state machines and callbacks.



C#'s had closures and anonymous functions for almost a decade, Java's got them properly now with Java 8, but you could always hack them with anonymous inner classes. C++ got them in C++11, etc. Hell, even in C, you could hack something resembling a closure with a struct and a function pointer.


PHP has closures.


Pseudo-closures. They capture variables by value, though you can use PHP's referencing mechanism to have something sort of closure-like.


There is no such thing as an "anonymous closure". There are only anonymous functions which may or may not create a closure. Early LISPs either had no way of creating closures, or did so through some sort of function or other mechanism. It wasn't until Scheme came around that lexical closures really became a thing in Lisp world.

Perl and PHP both have closures. In fact, Perl is closer to Scheme/Lisp than most languages (at least Perl doesn't screw up scoping like JavaScript, replace first-class functions with second-class-bastardizations like Ruby, or do whatever Python thinks it's doing with lambda... ugh)


    #!/usr/bin/node

    function main() {
      var a = 1;
      console.log(a);
    
      // named closure
      function foo() {
        a = a + 1;
      }
    
      foo();
      console.log(a);
    
      // anonymous closure
      (function() {
        a = a + 1;
      })();
    
      console.log(a);
    }
    main();


I'll emphasize GPs words:

> There is no such thing as an "anonymous closure". There are only anonymous functions which may or may not create a closure.

Which is exactly what you did: calling an anonymous function which happens to create a closure over variable `a`.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: