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

In Perl, for reference:

  use strict; use warnings;

  sub counter {
        my $n = $_[0];
        return sub { ++$n }
  }

  my $c = counter( 5 );
  say $c->();
  say $c->();


And its also handy that you can create a closure without needing to define a subroutine:

  my $c = do {
      my $n = 5;
      sub { ++$n }
  };




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

Search: