SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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
 

Ähnlich wie 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
 

Ähnlich wie 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 ...
 

Mehr von 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
 

Mehr von 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
 

Kürzlich hochgeladen

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
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 MenDelhi Call girls
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
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 2024The Digital Insurer
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
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 AutomationSafe Software
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
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 Nanonetsnaman860154
 
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 productivityPrincipled Technologies
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
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
 
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
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
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
 

Kürzlich hochgeladen (20)

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
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
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
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
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
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
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
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
 
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
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
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
 
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
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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
 

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