There's more than one way to empty it

2,002 views
1,937 views

Published on

Funny story of how Perl people can clear an array.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,002
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

There's more than one way to empty it

  1. 1. There’s more than one way to empty it
  2. 2. Perlish more-than-one-way
  3. 3. We hired Delphi/SQL programmer
  4. 4. as a Perl developer
  5. 5. Good programmer writes good code in every language
  6. 6. But lots of fun
  7. 7. sub clear{    my $template = shift @args;    @{$self->{tokens}} = ();    foreach (map {$_->{name}} @{$template->{si}}){        while (my $r = shift @{$self->{$_}->{res}}){}    } }
  8. 8. Che cazzo di..?* * WTF? (Italian)
  9. 9. sub clear{    my $template = shift @args;    @{$self->{tokens}} = ();    foreach (map {$_->{name}} @{$template->{si}}){        while (my $r = shift @{$self->{$_}->{res}}){}    } } A subroutine to make an object empty
  10. 10. sub clear{    my $template = shift @args;    @{$self->{tokens}} = ();    foreach (map {$_->{name}} @{$template->{si}}){        while (my $r = shift @{$self->{$_}->{res}}){}    } } Note this
  11. 11. Posted that piece to Moscow.pm mailing list
  12. 12. And we bacame crazy
  13. 13. 1 while (my $r = shift @a) {}
  14. 14. 2 splice @a;
  15. 15. 3 delete(@a[0..$#a]);
  16. 16. 4 grep {shift @a} @a;
  17. 17. 5 @a = ();
  18. 18. 5 @a = (); Boring!
  19. 19. 6 undef @a;
  20. 20. ccn@ccn-laptop ~ $ perl -le 'my @a=(1,2,3); while(my $r = shift @a){}; print @a' ccn@ccn-laptop ~ $ perl -le 'my @a=(0,1,2,3); while(my $r = shift @a){}; print @a' 123
  21. 21. 7 while(defined(my $r = shift @a)) {}
  22. 22. ccn@ccn-laptop ~ $ perl -le 'my @a=(undef,1,2,3);while (defined(my $r=shift @a)){};print @a' 123
  23. 23. Doesn’t work
  24. 24. Let’s make it useful
  25. 25. $str = "abcc{0}def"; @a = split //, $str; $c++ while(shift @a); say $c - 1; Calculate zero-ending string length
  26. 26. 8 while(chomp $str) {};
  27. 27. 9 while(@a) {shift @a}
  28. 28. 10 shift @a while @a; Much better!
  29. 29. 11 shift @a while @a and die;
  30. 30. 12 shift @a while scalar @a;
  31. 31. 13 $#a = -1 Who remembers?!
  32. 32. use Benchmark qw(:all :hireswallclock); my $size = 1000; Be cmpthese timethese -3, { nc    '@a = ...'          => sub { my @a = (('test')x$size); h m        return; }, ar    'undef @a'          => sub { my @a = (('test')x$size); undef @a; ke        return; }, d    '@a=()'             => sub { my @a = (('test')x$size); @a = ();        return; },    'splice @a'         => sub { my @a = (('test')x$size); splice @a;        return; },    '$#a = -1'          => sub { my @a = (('test')x$size); $#a = -1;        return; },    'shift @a while @a' => sub { my @a = (('test')x$size); shift @a while @a;  return; },    'delete @a[0..#$a]' => sub { my @a = (('test')x$size); delete(@a[0..$#a]); return; }, }; при $size=1000; shift @a while @a 2642/s                --              -22%      -44%    -44%  -45%     -45%     -45% delete @a[0..#$a] 3376/s               28%                --      -28%    -29%  -29%     -30%     -30% splice @a         4722/s               79%               40%        --     -0%   -1%      -2%      -2%
  33. 33. TIMTOWTDI or die; Andrew Shitov talks.shitov.ru | andy@shitov.ru

×