SlideShare uma empresa Scribd logo
1 de 34
Baixar para ler offline
There’s more
 than one way
to empty it
Perlish
more-than-one-way
We hired
 Delphi/SQL
programmer
as a Perl developer
Good programmer
writes good code
in every language
But lots of fun
sub clear{
   my $template = shift @args;
   @{$self->{tokens}} = ();
   foreach (map {$_->{name}} @{$template->{si}}){
       while (my $r = shift @{$self->{$_}->{res}}){}
   }
}
Che cazzo di..?*


   * WTF? (Italian)
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
sub clear{
   my $template = shift @args;
   @{$self->{tokens}} = ();
   foreach (map {$_->{name}} @{$template->{si}}){
       while (my $r = shift @{$self->{$_}->{res}}){}
   }
}



                    Note this
Posted that piece to
Moscow.pm mailing list
And we bacame crazy
1

while (my $r = shift @a) {}
2

splice @a;
3

delete(@a[0..$#a]);
4

grep {shift @a} @a;
5

@a = ();
5

@a = ();



Boring!
6

undef @a;
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
7

while(defined(my $r = shift @a))
              {}
ccn@ccn-laptop ~
$ perl -le 'my @a=(undef,1,2,3);while
(defined(my $r=shift @a)){};print @a'
123
Doesn’t work
Let’s make it
   useful
$str = "abcc{0}def";
@a = split //, $str;
$c++ while(shift @a);
say $c - 1;


Calculate zero-ending string length
8

while(chomp $str) {};
9

while(@a) {shift @a}
10

shift @a while @a;




    Much better!
11

shift @a while @a and die;
12

shift @a while scalar @a;
13

 $#a = -1




Who remembers?!
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%
TIMTOWTDI or die;




         Andrew Shitov

talks.shitov.ru | andy@shitov.ru
There's more than one way to empty it

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
Camping
CampingCamping
Camping
 
Tres Gemas De Ruby
Tres Gemas De RubyTres Gemas De Ruby
Tres Gemas De Ruby
 
Cli deep dive
Cli deep diveCli deep dive
Cli deep dive
 
Elf文件解析
Elf文件解析Elf文件解析
Elf文件解析
 
From Javascript To Haskell
From Javascript To HaskellFrom Javascript To Haskell
From Javascript To Haskell
 
Hacking the Internet of Things for Fun & Profit
Hacking the Internet of Things for Fun & ProfitHacking the Internet of Things for Fun & Profit
Hacking the Internet of Things for Fun & Profit
 
New features in Ruby 2.4
New features in Ruby 2.4New features in Ruby 2.4
New features in Ruby 2.4
 
Perlで任意精度計算
Perlで任意精度計算Perlで任意精度計算
Perlで任意精度計算
 
Awk hints
Awk hintsAwk hints
Awk hints
 
Mpi cheat sheet
Mpi cheat sheetMpi cheat sheet
Mpi cheat sheet
 
Bubble Sort
Bubble SortBubble Sort
Bubble Sort
 
C programs
C programsC programs
C programs
 
Perl6 one-liners
Perl6 one-linersPerl6 one-liners
Perl6 one-liners
 
Sol9
Sol9Sol9
Sol9
 
Use of django at jolt online v3
Use of django at jolt online v3Use of django at jolt online v3
Use of django at jolt online v3
 
3分くらいで分かるassert()
3分くらいで分かるassert()3分くらいで分かるassert()
3分くらいで分かるassert()
 
Watch out: Observables are here to stay
Watch out: Observables are here to stayWatch out: Observables are here to stay
Watch out: Observables are here to stay
 
bank management system
bank management systembank management system
bank management system
 
C questions
C questionsC questions
C questions
 

Destaque

Finding Your Best Investigative Business Story by Michael J. Berens
Finding Your Best Investigative Business Story by Michael J. BerensFinding Your Best Investigative Business Story by Michael J. Berens
Finding Your Best Investigative Business Story by Michael J. Berens
Reynolds Center for Business Journalism
 
True love
 True love  True love
True love
Xenia Y
 
A strong woman versus a woman of strength
A strong woman versus a woman of strengthA strong woman versus a woman of strength
A strong woman versus a woman of strength
Xenia Y
 
Cookies PowerPoint
Cookies PowerPointCookies PowerPoint
Cookies PowerPoint
emurfield
 

Destaque (14)

A BISCUIT STORY
A BISCUIT STORYA BISCUIT STORY
A BISCUIT STORY
 
Airport Cookie
Airport CookieAirport Cookie
Airport Cookie
 
Interesting Story
Interesting StoryInteresting Story
Interesting Story
 
Finding Your Best Investigative Business Story by Michael J. Berens
Finding Your Best Investigative Business Story by Michael J. BerensFinding Your Best Investigative Business Story by Michael J. Berens
Finding Your Best Investigative Business Story by Michael J. Berens
 
HEART ATTACK BEWARE
HEART ATTACK BEWAREHEART ATTACK BEWARE
HEART ATTACK BEWARE
 
COOL ATTITUDES
COOL ATTITUDESCOOL ATTITUDES
COOL ATTITUDES
 
Toiletcleaner11
Toiletcleaner11Toiletcleaner11
Toiletcleaner11
 
NELSON MANDELA: WORLD LEADER AND HERO
NELSON MANDELA: WORLD LEADER AND HERONELSON MANDELA: WORLD LEADER AND HERO
NELSON MANDELA: WORLD LEADER AND HERO
 
True love
 True love  True love
True love
 
A strong woman versus a woman of strength
A strong woman versus a woman of strengthA strong woman versus a woman of strength
A strong woman versus a woman of strength
 
The lottery story_reported_questions
The lottery story_reported_questionsThe lottery story_reported_questions
The lottery story_reported_questions
 
GOD AND WOMAN
GOD AND WOMANGOD AND WOMAN
GOD AND WOMAN
 
Reported speech the_lottery_story_part1
Reported speech the_lottery_story_part1Reported speech the_lottery_story_part1
Reported speech the_lottery_story_part1
 
Cookies PowerPoint
Cookies PowerPointCookies PowerPoint
Cookies PowerPoint
 

Semelhante a There's more than one way to empty it

Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
Kang-min Liu
 
20 modules i haven't yet talked about
20 modules i haven't yet talked about20 modules i haven't yet talked about
20 modules i haven't yet talked about
Tatsuhiko Miyagawa
 
perl usage at database applications
perl usage at database applicationsperl usage at database applications
perl usage at database applications
Joe Jiang
 
you need to complete the r code and a singlepage document c.pdf
you need to complete the r code and a singlepage document c.pdfyou need to complete the r code and a singlepage document c.pdf
you need to complete the r code and a singlepage document c.pdf
adnankhan605720
 
php global $bsize,$playerToken,$myToken,$gameOver,$winArr,$rowAr.pdf
php global $bsize,$playerToken,$myToken,$gameOver,$winArr,$rowAr.pdfphp global $bsize,$playerToken,$myToken,$gameOver,$winArr,$rowAr.pdf
php global $bsize,$playerToken,$myToken,$gameOver,$winArr,$rowAr.pdf
anjalitimecenter11
 
Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB
jhchabran
 

Semelhante a There's more than one way to empty it (20)

Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrow
 
Php functions
Php functionsPhp functions
Php functions
 
20 modules i haven't yet talked about
20 modules i haven't yet talked about20 modules i haven't yet talked about
20 modules i haven't yet talked about
 
perl usage at database applications
perl usage at database applicationsperl usage at database applications
perl usage at database applications
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
 
Bag of tricks
Bag of tricksBag of tricks
Bag of tricks
 
Perl Bag of Tricks - Baltimore Perl mongers
Perl Bag of Tricks  -  Baltimore Perl mongersPerl Bag of Tricks  -  Baltimore Perl mongers
Perl Bag of Tricks - Baltimore Perl mongers
 
The Perl6 Type System
The Perl6 Type SystemThe Perl6 Type System
The Perl6 Type System
 
全裸でワンライナー(仮)
全裸でワンライナー(仮)全裸でワンライナー(仮)
全裸でワンライナー(仮)
 
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
 
you need to complete the r code and a singlepage document c.pdf
you need to complete the r code and a singlepage document c.pdfyou need to complete the r code and a singlepage document c.pdf
you need to complete the r code and a singlepage document c.pdf
 
Php2
Php2Php2
Php2
 
php global $bsize,$playerToken,$myToken,$gameOver,$winArr,$rowAr.pdf
php global $bsize,$playerToken,$myToken,$gameOver,$winArr,$rowAr.pdfphp global $bsize,$playerToken,$myToken,$gameOver,$winArr,$rowAr.pdf
php global $bsize,$playerToken,$myToken,$gameOver,$winArr,$rowAr.pdf
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
 
Signal Stacktrace
Signal StacktraceSignal Stacktrace
Signal Stacktrace
 
Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB
 
Adventures in Optimization
Adventures in OptimizationAdventures in Optimization
Adventures in Optimization
 
Functional Pe(a)rls version 2
Functional Pe(a)rls version 2Functional Pe(a)rls version 2
Functional Pe(a)rls version 2
 

Mais de Andrew Shitov

The Joy of Smartmatch
The Joy of SmartmatchThe Joy of Smartmatch
The Joy of Smartmatch
Andrew Shitov
 

Mais de Andrew Shitov (20)

Creating a compiler in Perl 6
Creating a compiler in Perl 6Creating a compiler in Perl 6
Creating a compiler in Perl 6
 
Fun with Raspberry PI (and Perl)
Fun with Raspberry PI (and Perl)Fun with Raspberry PI (and Perl)
Fun with Raspberry PI (and Perl)
 
Perl6 in-production
Perl6 in-productionPerl6 in-production
Perl6 in-production
 
Параллельные вычисления в Perl 6
Параллельные вычисления в Perl 6Параллельные вычисления в Perl 6
Параллельные вычисления в Perl 6
 
AllPerlBooks.com
AllPerlBooks.comAllPerlBooks.com
AllPerlBooks.com
 
Perl 6 for Concurrency and Parallel Computing
Perl 6 for Concurrency and Parallel ComputingPerl 6 for Concurrency and Parallel Computing
Perl 6 for Concurrency and Parallel Computing
 
The Joy of Smartmatch
The Joy of SmartmatchThe Joy of Smartmatch
The Joy of Smartmatch
 
YAPC::Europe 2013
YAPC::Europe 2013YAPC::Europe 2013
YAPC::Europe 2013
 
Perl 7, the story of
Perl 7, the story ofPerl 7, the story of
Perl 7, the story of
 
Язык программирования Go для Perl-программистов
Язык программирования Go для Perl-программистовЯзык программирования Go для Perl-программистов
Язык программирования Go для Perl-программистов
 
Как очистить массив
Как очистить массивКак очистить массив
Как очистить массив
 
What's new in Perl 5.14
What's new in Perl 5.14What's new in Perl 5.14
What's new in Perl 5.14
 
Что нового в Perl 5.14
Что нового в Perl 5.14Что нового в Perl 5.14
Что нового в Perl 5.14
 
Perl6 grammars
Perl6 grammarsPerl6 grammars
Perl6 grammars
 
Text in search queries with examples in Perl 6
Text in search queries with examples in Perl 6Text in search queries with examples in Perl 6
Text in search queries with examples in Perl 6
 
Perl 6 by example
Perl 6 by examplePerl 6 by example
Perl 6 by example
 
Perl 5.10 и 5.12
Perl 5.10 и 5.12Perl 5.10 и 5.12
Perl 5.10 и 5.12
 
Say Perl на весь мир
Say Perl на весь мирSay Perl на весь мир
Say Perl на весь мир
 
Personal Perl 6 compiler
Personal Perl 6 compilerPersonal Perl 6 compiler
Personal Perl 6 compiler
 
Perl 5.10 in 2010
Perl 5.10 in 2010Perl 5.10 in 2010
Perl 5.10 in 2010
 

Último

Último (20)

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 

There's more than one way to empty it