SlideShare a Scribd company logo
1 of 24
Perl's Functional Functions
The common (core) ones...
● Built-in:
– grep – Filters a list.
– map – Creates or transforms a list.
– sort – Sorts a list.
● List::Util (core)
– first – Like grep, but returns first one found.
– reduce – Summarizes a list.
The common CPAN ones:
● List::MoreUtils
– any – Return true if any element in list matches.
– all – Return true if all elements match.
– pairwise – Transform two lists, pairwise, into one.
– Many others!
Filtering a list
my @odds;
foreach ( qw( a b c d e ) ) {
push @filtered, $_
if ord( $_ ) % 2;
}
say for @odds;
Filtering a list with grep
my @odds;
foreach (
qw( a b c d e )
) {
push @odds, $_
if ord( $_ ) % 2;
}
say for @odds;
my @odds = grep {
ord( $_ ) % 2
} qw( a b c d e );
say for @odds;
my @odds = grep { ord($_) % 2 } qw( a b c d e );
Which indices contain odd ords?
my @chars = qw( a c f b q r n b d );
my @odds_by_idx = grep {
ord( $chars[$_] ) % 2
} 0 .. $#chars;
grep
● list_b = grep { code block } ( list_a )
● Inside code block, $_ is it.
● If code block's return value is true, $_ is appended to
list_b.
● As with foreach, $_ is an alias.
● list_b and list_a need not be arrays.
● Simple expressions may be used in place of code
block.
Simple expressions in place of code
block
@explosions = grep { /bkaboomb/i } @phrases;
@explosions = grep /bkaboomb/i, @phrases;
@ones = grep { $_ == 1 } @booleans;
@ones = grep $_ == 1, @booleans;
● If it can't be expressed as a simple expression,
use a code block.
● Expression form requires a comma.
Transforming a list
sub chars_to_ords {
my @ords;
foreach ( @_ ) {
push @ords, ord $_;
}
return @ords;
}
my @ord_vals = chars_to_ords( qw( a b c d e ) );
Transforming a list with map
sub chars_to_ords {
my @ords;
foreach ( @_ ) {
push @ords, ord $_;
}
return @ords;
}
my @ordinals
= chars_to_ords(
qw( a b c d e )
);
my @ordinals
= map { ord $_ }
qw( a b c d e );
my @ordinals = map { ord $_ } qw( a b c d e );
Lists don't have to be arrays
say for map { ord } qw( a b c d e );
map
● list_b = map { code block } ( list_a )
● Inside code block, $_ is it.
● code block's return value is appended to list_b.
● As with foreach, $_ is an alias.
● list_b and list_a need not be arrays.
● code block is a subroutine; no last, no next.
● Skip current iteration by returning an empty list.
Simple expressions don't require
code blocks.
say for map ord, qw( a b c d e );
# The expression form requires a comma.
Chaining is legal (even encouraged)
say for
map { $_->[0] }
sort { $a->[1] <=> $b->[1] }
map { [ $_, ord fc $_ ] }
qw( a b c d e );
# The Schwartzian Transform.
Sort?
@sorted = sort @unsorted;
@sorted = sort { $a <=> $b } @unsorted; # Ascend, numeric
@sorted = sort { $b <=> $a } @unsorted; # Descend, numeric
@sorted = sort { $a cmp $b } @unsorted; # Ascend, stringy
@sorted = sort {
$a <=> $b || $a cmp $b
} @unsorted; # Ascending numeric, then stringy.
@sorted
= sort { $a->{name} cmp $b->{name} } @unsorted;
List::Util
$first_one = first { /^kaboomb/i } @haystack;
$sum = reduce { $a + $b } @numbers;
$max = reduce { $a > $b ? $a : $b } @numbers;
List::MoreUtils
$has_quiche = any { /bquicheb/ } @foods; # T/F
$all_unicorns
= all { $_ eq 'unicorn' } @animals; # T/F
@joint_incomes = pairwise { $a + $b } @his, @hers;
@evens_odds = part { $_ % 2 } 1 .. 9; # LOLs
List::MoreUtils “natatime”
my @alphabet = ( 'a' .. 'z' );
my $iterator = natatime 3, @alphabet;
while( my @chars = $iterator->() ) {
print “@charsn”;
}
__END__
a b c
d e f
…
y z
List::MoreUtils
● Too many to mention them all.
– See https://metacpan.org/module/List::MoreUtils
Resources
● perldoc -f grep
● perldoc -f map
● perldoc -f sort
● perldoc List::Util
● https://metacpan.org/module/List::MoreUtils
Salt Lake Perl Mongers
http://saltlake.pm.org
Slides on slideshare.net
http://www.slideshare.net/daoswald/perls-functional-functions
Dave Oswald
davido@cpan.org

More Related Content

What's hot

Php String And Regular Expressions
Php String  And Regular ExpressionsPhp String  And Regular Expressions
Php String And Regular Expressionsmussawir20
 
Perl names values and variables
Perl names values and variablesPerl names values and variables
Perl names values and variablessana mateen
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlsana mateen
 
Regular expression in javascript
Regular expression in javascriptRegular expression in javascript
Regular expression in javascriptToan Nguyen
 
Merging tables using R
Merging tables using R Merging tables using R
Merging tables using R Rupak Roy
 
Power shell basics day 5
Power shell basics day 5Power shell basics day 5
Power shell basics day 5Ashish Raj
 
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 tomorrowPete McFarlane
 
Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)Bozhidar Boshnakov
 

What's hot (14)

Php String And Regular Expressions
Php String  And Regular ExpressionsPhp String  And Regular Expressions
Php String And Regular Expressions
 
Perl names values and variables
Perl names values and variablesPerl names values and variables
Perl names values and variables
 
PHP 101
PHP 101 PHP 101
PHP 101
 
Pig statements
Pig statementsPig statements
Pig statements
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perl
 
Regular expression in javascript
Regular expression in javascriptRegular expression in javascript
Regular expression in javascript
 
Merging tables using R
Merging tables using R Merging tables using R
Merging tables using R
 
Power shell basics day 5
Power shell basics day 5Power shell basics day 5
Power shell basics day 5
 
Php2
Php2Php2
Php2
 
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
 
Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)
 
Perl6 signatures
Perl6 signaturesPerl6 signatures
Perl6 signatures
 
Strings and pointers
Strings and pointersStrings and pointers
Strings and pointers
 
Introduction in php part 2
Introduction in php part 2Introduction in php part 2
Introduction in php part 2
 

Similar to Perls Functional functions

Scripting3
Scripting3Scripting3
Scripting3Nao Dara
 
Writing Maintainable Perl
Writing Maintainable PerlWriting Maintainable Perl
Writing Maintainable Perltinypigdotcom
 
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 & Perl6Workhorse Computing
 
Introduction to Perl - Day 2
Introduction to Perl - Day 2Introduction to Perl - Day 2
Introduction to Perl - Day 2Dave Cross
 
There's more than one way to empty it
There's more than one way to empty itThere's more than one way to empty it
There's more than one way to empty itAndrew Shitov
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to PerlSway Wang
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regexJalpesh Vasa
 
Unit 1-array,lists and hashes
Unit 1-array,lists and hashesUnit 1-array,lists and hashes
Unit 1-array,lists and hashessana mateen
 
Programming in perl style
Programming in perl styleProgramming in perl style
Programming in perl styleBo Hua Yang
 
How to clean an array
How to clean an arrayHow to clean an array
How to clean an arrayAndrew Shitov
 
Crash Course in Perl – Perl tutorial for C programmers
Crash Course in Perl – Perl tutorial for C programmersCrash Course in Perl – Perl tutorial for C programmers
Crash Course in Perl – Perl tutorial for C programmersGil Megidish
 
BSDM with BASH: Command Interpolation
BSDM with BASH: Command InterpolationBSDM with BASH: Command Interpolation
BSDM with BASH: Command InterpolationWorkhorse Computing
 
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...Dhivyaa C.R
 

Similar to Perls Functional functions (20)

Scripting3
Scripting3Scripting3
Scripting3
 
Writing Maintainable Perl
Writing Maintainable PerlWriting Maintainable Perl
Writing Maintainable Perl
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
 
Lists and arrays
Lists and arraysLists and arrays
Lists and arrays
 
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
 
Introduction to Perl - Day 2
Introduction to Perl - Day 2Introduction to Perl - Day 2
Introduction to Perl - Day 2
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
 
There's more than one way to empty it
There's more than one way to empty itThere's more than one way to empty it
There's more than one way to empty it
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regex
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Unit 1-array,lists and hashes
Unit 1-array,lists and hashesUnit 1-array,lists and hashes
Unit 1-array,lists and hashes
 
Programming in perl style
Programming in perl styleProgramming in perl style
Programming in perl style
 
Barcelona.pm Curs1211 sess01
Barcelona.pm Curs1211 sess01Barcelona.pm Curs1211 sess01
Barcelona.pm Curs1211 sess01
 
How to clean an array
How to clean an arrayHow to clean an array
How to clean an array
 
Crash Course in Perl – Perl tutorial for C programmers
Crash Course in Perl – Perl tutorial for C programmersCrash Course in Perl – Perl tutorial for C programmers
Crash Course in Perl – Perl tutorial for C programmers
 
BSDM with BASH: Command Interpolation
BSDM with BASH: Command InterpolationBSDM with BASH: Command Interpolation
BSDM with BASH: Command Interpolation
 
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
 

More from daoswald

Perl: Setting Up An Internal Darkpan
Perl: Setting Up An Internal DarkpanPerl: Setting Up An Internal Darkpan
Perl: Setting Up An Internal Darkpandaoswald
 
Speaking at Tech Events
Speaking at Tech EventsSpeaking at Tech Events
Speaking at Tech Eventsdaoswald
 
Perl one-liners
Perl one-linersPerl one-liners
Perl one-linersdaoswald
 
Whatsnew in-perl
Whatsnew in-perlWhatsnew in-perl
Whatsnew in-perldaoswald
 
Add Perl to Your Toolbelt
Add Perl to Your ToolbeltAdd Perl to Your Toolbelt
Add Perl to Your Toolbeltdaoswald
 
Think Like a Programmer
Think Like a ProgrammerThink Like a Programmer
Think Like a Programmerdaoswald
 
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?Regular Expressions: Backtracking, and The Little Engine that Could(n't)?
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?daoswald
 
Getting started with Perl XS and Inline::C
Getting started with Perl XS and Inline::CGetting started with Perl XS and Inline::C
Getting started with Perl XS and Inline::Cdaoswald
 
30 Minutes To CPAN
30 Minutes To CPAN30 Minutes To CPAN
30 Minutes To CPANdaoswald
 
Deploying Perl apps on dotCloud
Deploying Perl apps on dotCloudDeploying Perl apps on dotCloud
Deploying Perl apps on dotClouddaoswald
 

More from daoswald (10)

Perl: Setting Up An Internal Darkpan
Perl: Setting Up An Internal DarkpanPerl: Setting Up An Internal Darkpan
Perl: Setting Up An Internal Darkpan
 
Speaking at Tech Events
Speaking at Tech EventsSpeaking at Tech Events
Speaking at Tech Events
 
Perl one-liners
Perl one-linersPerl one-liners
Perl one-liners
 
Whatsnew in-perl
Whatsnew in-perlWhatsnew in-perl
Whatsnew in-perl
 
Add Perl to Your Toolbelt
Add Perl to Your ToolbeltAdd Perl to Your Toolbelt
Add Perl to Your Toolbelt
 
Think Like a Programmer
Think Like a ProgrammerThink Like a Programmer
Think Like a Programmer
 
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?Regular Expressions: Backtracking, and The Little Engine that Could(n't)?
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?
 
Getting started with Perl XS and Inline::C
Getting started with Perl XS and Inline::CGetting started with Perl XS and Inline::C
Getting started with Perl XS and Inline::C
 
30 Minutes To CPAN
30 Minutes To CPAN30 Minutes To CPAN
30 Minutes To CPAN
 
Deploying Perl apps on dotCloud
Deploying Perl apps on dotCloudDeploying Perl apps on dotCloud
Deploying Perl apps on dotCloud
 

Recently uploaded

FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
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.pptxHampshireHUG
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
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 MenDelhi Call girls
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 

Recently uploaded (20)

FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
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
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
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
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 

Perls Functional functions

  • 2. The common (core) ones... ● Built-in: – grep – Filters a list. – map – Creates or transforms a list. – sort – Sorts a list. ● List::Util (core) – first – Like grep, but returns first one found. – reduce – Summarizes a list.
  • 3. The common CPAN ones: ● List::MoreUtils – any – Return true if any element in list matches. – all – Return true if all elements match. – pairwise – Transform two lists, pairwise, into one. – Many others!
  • 4. Filtering a list my @odds; foreach ( qw( a b c d e ) ) { push @filtered, $_ if ord( $_ ) % 2; } say for @odds;
  • 5. Filtering a list with grep my @odds; foreach ( qw( a b c d e ) ) { push @odds, $_ if ord( $_ ) % 2; } say for @odds; my @odds = grep { ord( $_ ) % 2 } qw( a b c d e ); say for @odds;
  • 6. my @odds = grep { ord($_) % 2 } qw( a b c d e );
  • 7. Which indices contain odd ords? my @chars = qw( a c f b q r n b d ); my @odds_by_idx = grep { ord( $chars[$_] ) % 2 } 0 .. $#chars;
  • 8. grep ● list_b = grep { code block } ( list_a ) ● Inside code block, $_ is it. ● If code block's return value is true, $_ is appended to list_b. ● As with foreach, $_ is an alias. ● list_b and list_a need not be arrays. ● Simple expressions may be used in place of code block.
  • 9. Simple expressions in place of code block @explosions = grep { /bkaboomb/i } @phrases; @explosions = grep /bkaboomb/i, @phrases; @ones = grep { $_ == 1 } @booleans; @ones = grep $_ == 1, @booleans; ● If it can't be expressed as a simple expression, use a code block. ● Expression form requires a comma.
  • 10. Transforming a list sub chars_to_ords { my @ords; foreach ( @_ ) { push @ords, ord $_; } return @ords; } my @ord_vals = chars_to_ords( qw( a b c d e ) );
  • 11. Transforming a list with map sub chars_to_ords { my @ords; foreach ( @_ ) { push @ords, ord $_; } return @ords; } my @ordinals = chars_to_ords( qw( a b c d e ) ); my @ordinals = map { ord $_ } qw( a b c d e );
  • 12. my @ordinals = map { ord $_ } qw( a b c d e );
  • 13. Lists don't have to be arrays say for map { ord } qw( a b c d e );
  • 14. map ● list_b = map { code block } ( list_a ) ● Inside code block, $_ is it. ● code block's return value is appended to list_b. ● As with foreach, $_ is an alias. ● list_b and list_a need not be arrays. ● code block is a subroutine; no last, no next. ● Skip current iteration by returning an empty list.
  • 15. Simple expressions don't require code blocks. say for map ord, qw( a b c d e ); # The expression form requires a comma.
  • 16. Chaining is legal (even encouraged) say for map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [ $_, ord fc $_ ] } qw( a b c d e ); # The Schwartzian Transform.
  • 17. Sort? @sorted = sort @unsorted; @sorted = sort { $a <=> $b } @unsorted; # Ascend, numeric @sorted = sort { $b <=> $a } @unsorted; # Descend, numeric @sorted = sort { $a cmp $b } @unsorted; # Ascend, stringy @sorted = sort { $a <=> $b || $a cmp $b } @unsorted; # Ascending numeric, then stringy. @sorted = sort { $a->{name} cmp $b->{name} } @unsorted;
  • 18. List::Util $first_one = first { /^kaboomb/i } @haystack; $sum = reduce { $a + $b } @numbers; $max = reduce { $a > $b ? $a : $b } @numbers;
  • 19. List::MoreUtils $has_quiche = any { /bquicheb/ } @foods; # T/F $all_unicorns = all { $_ eq 'unicorn' } @animals; # T/F @joint_incomes = pairwise { $a + $b } @his, @hers; @evens_odds = part { $_ % 2 } 1 .. 9; # LOLs
  • 20. List::MoreUtils “natatime” my @alphabet = ( 'a' .. 'z' ); my $iterator = natatime 3, @alphabet; while( my @chars = $iterator->() ) { print “@charsn”; } __END__ a b c d e f … y z
  • 21. List::MoreUtils ● Too many to mention them all. – See https://metacpan.org/module/List::MoreUtils
  • 22. Resources ● perldoc -f grep ● perldoc -f map ● perldoc -f sort ● perldoc List::Util ● https://metacpan.org/module/List::MoreUtils
  • 23. Salt Lake Perl Mongers http://saltlake.pm.org Slides on slideshare.net http://www.slideshare.net/daoswald/perls-functional-functions