fork download
  1. #!/usr/bin/perl
  2. # your code goes here
  3.  
  4. sub makeIteratorClass {
  5. my ($f) = @_;
  6.  
  7. sub gensym {
  8. my @chars = ("A".."Z", "a".."z");
  9. my $string;
  10. $string .= $chars[rand @chars] for 1..8;
  11. return $string;
  12. };
  13.  
  14. return sub {
  15. my ($iv) = @_;
  16.  
  17. my $self = { iv => $iv };
  18. my $sym = gensym();
  19. *{ "$sym" . "::getNext" } = sub {
  20. my ($self) = @_;
  21. $self->{iv} = $f->($self->{iv});
  22. return $self->{iv};
  23. };
  24. bless $self, $sym;
  25. return $self;
  26. }
  27. }
  28.  
  29. my $plusOner = makeIteratorClass(sub { my ($x) = @_; $x + 1 });
  30. my $o = $plusOner->(5);
  31. print $o->getNext();
  32. print $o->getNext();
  33. print $o->getNext();
Success #stdin #stdout 0s 3564KB
stdin
Standard input is empty
stdout
678