Your SlideShare is downloading. ×
0
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
There's more than one way to empty it
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

There's more than one way to empty it

1,738

Published on

Funny story of how Perl people can clear an array.

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
1,738
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. There’s more than one way to empty it
  • 2. Perlish more-than-one-way
  • 3. We hired Delphi/SQL programmer
  • 4. as a Perl developer
  • 5. Good programmer writes good code in every language
  • 6. But lots of fun
  • 7. sub clear{    my $template = shift @args;    @{$self->{tokens}} = ();    foreach (map {$_->{name}} @{$template->{si}}){        while (my $r = shift @{$self->{$_}->{res}}){}    } }
  • 8. Che cazzo di..?* * WTF? (Italian)
  • 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. sub clear{    my $template = shift @args;    @{$self->{tokens}} = ();    foreach (map {$_->{name}} @{$template->{si}}){        while (my $r = shift @{$self->{$_}->{res}}){}    } } Note this
  • 11. Posted that piece to Moscow.pm mailing list
  • 12. And we bacame crazy
  • 13. 1 while (my $r = shift @a) {}
  • 14. 2 splice @a;
  • 15. 3 delete(@a[0..$#a]);
  • 16. 4 grep {shift @a} @a;
  • 17. 5 @a = ();
  • 18. 5 @a = (); Boring!
  • 19. 6 undef @a;
  • 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. 7 while(defined(my $r = shift @a)) {}
  • 22. ccn@ccn-laptop ~ $ perl -le 'my @a=(undef,1,2,3);while (defined(my $r=shift @a)){};print @a' 123
  • 23. Doesn’t work
  • 24. Let’s make it useful
  • 25. $str = "abcc{0}def"; @a = split //, $str; $c++ while(shift @a); say $c - 1; Calculate zero-ending string length
  • 26. 8 while(chomp $str) {};
  • 27. 9 while(@a) {shift @a}
  • 28. 10 shift @a while @a; Much better!
  • 29. 11 shift @a while @a and die;
  • 30. 12 shift @a while scalar @a;
  • 31. 13 $#a = -1 Who remembers?!
  • 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. TIMTOWTDI or die; Andrew Shitov talks.shitov.ru | andy@shitov.ru

×