SlideShare une entreprise Scribd logo
1  sur  95
Télécharger pour lire hors ligne
Perl 6
One-Liners
Andrew Shitov


German Perl Workshop

Munich, 8 March 2019
A PLAY IN EIGHT ACTS
Rakudo
command-line
options1
-e
execute
$ perl6 -e'say 42'
$ perl6 -e'$*PERL.version.say'
v6.d
-e, not -E
-n
repeat for each input line
$ perl6 -ne'say [+] .split(" ")' 
data.txt
-p
repeat and print
$ perl6 -npe'.=flip' data.txt
Perl 6 am MAIN
2
multi sub MAIN() {
say "No args"
}
multi sub MAIN($x) {
say "Single arg $x"
}
multi sub MAIN($x, $y) {
say "Two args: $x and $y"
}
$ perl6 main.pl
No args
$ perl6 main.pl 1
Single arg 1
$ perl6 main.pl 1 2
Two args: 1 and 2
$ perl6 main.pl 1 2 3
Usage:
main.pl <x> <y>
multi sub MAIN(Int $x) {
say "Integer $x"
}
multi sub MAIN(Str $s) {
say "String $s"
}
$ perl6 main2.pl abc
String abc
$ perl6 main2.pl 42
Ambiguous call to 'MAIN(IntStr)';
these signatures all match:
:(Int $x)
:(Str $s)
in block <unit> at main2.pl line 5
multi sub MAIN(IntStr $x) {
say "Integer $x"
}
multi sub MAIN(Str $s) {
say "String $s"
}
multi sub MAIN(*@args) {
say "Passed {@args.elems} args"
}
$ perl6 main3.pl
Passed 0 args
$ perl6 main3.pl 1
Passed 1 args
$ perl6 main3.pl a b c d
Passed 4 args
Mangling

Text

Files3
8 exercises
$ perl6 -npe's/$/n/' text.txt
$ perl6 -npe's/$/n/' text.txt
Double-space a file
$ perl6 -ne'.say if .chars' text.txt
$ perl6 -ne'.say if .chars' text.txt
Remove all blank lines
$ perl6 -ne'.say if /S/' text.txt
$ perl6 -ne'.say if /S/' text.txt
Remove all blank lines
docs.perl6.org/language/variables#The_$_variable
$ perl6 -ne'say ++$ ~ ". " ~ $_' text.txt
Number all lines in a file
$ perl6 -ne'say ++$ ~ ". " ~ $_' text.txt
$ perl6 -npe'.=uc' text.txt
Convert all text to uppercase
$ perl6 -npe'.=uc' text.txt
$ perl6 -npe'.=trim' text.txt
Strip whitespaces
$ perl6 -npe'.=trim' text.txt
$ perl6 -ne'.say ; exit' text.txt
Print the first line of a file
$ perl6 -ne'.say ; exit' text.txt
$ perl6 -npe'exit if $++ == 10' text.txt
Print the first 10 lines of a file
$ perl6 -npe'exit if $++ == 10' text.txt
Reverse a file
.say for lines.reverse
$ perl6 reverse.pl < text.txt
Reverse a file
.say for $*IN.lines.reverse
$ perl6 reverse.pl < text.txt
Reverse a file
.say for
@*ARGS[0].IO.open.lines.reverse
$ perl6 reverse.pl text.txt
Reading from multiple files
.say for $*ARGFILES.lines
$ perl6 work.pl a.txt b.txt
Batch file renaming
@*ARGS[0..*-2].sort.map:
*.Str.IO.rename(++@*ARGS[*-1])
$ perl6 rename.pl *.jpg img_0000.jpg
Perl 6

Meta-
Operators4
Compute totals
put [Z+] lines.map: *.words
Merge into columns
.say for [Z~] @*ARGS.map: *.IO.lines;
$ perl6 merge.pl a.txt b.txt
Product table
1..10 X* 1..10
(1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21
24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6
12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32
40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70
80 90 100)
Factorial
say [*] 1..2019
Least common multiplier
say [lcm] 1..20
Rotate a matrix
[Z] <A B C>, <D E F>, <H I J>
((A D H) (B E I) (C F J))
Sequence

Operator

. . .5
Fibonacci numbers
0, 1, * + * ... *
Fibonacci numbers
.say for (0, 1, * + * ... *)[^31]
First 30 elements
Prime numbers
say ((1..*).grep: *.is-prime)[10000]
The 10001 prime numberst
Prime numbers
.say if .is-prime for ^100
Prime numbers below 100
The value of π
say π
The value of π
say pi
The value of π
say 4 * [+] (^1000).map({(-1) ** $_ / (2 * $_ + 1)})
The value of π
say 4 * [+] (1, -1, 1 … *) «/« 

(1, 3, 5 … 9999);
Area of a square
say π × $𝜌²
Generating

Random

Numbers6
Random password
('0'..'z').pick(15).join.say
Non-repeating characters
Random password
('0'..'z').roll(15).join.say
Characters may repeat
('0'..'z').join.say
0123456789:;<=>?@
ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`
abcdefghijklmnopqrstuvwxyz
Random integer
2019.rand.Int.say
0 to 2019
Random integer
2019.rand.rand.Int.say
Is this number more random? :-)
Solving
Euler
Problems7
1. Print the sum of all multiples

of 3 and 5 below 1000
say sum((1..999).grep: * %% (3 | 5))
1. Print the sum of all multiples

of 3 and 5 below 1000
sub f($n) {
($n <<*>> (1...1000 / $n)).grep: * < 1000
}
say (f(3) ∪ f(5)).keys.sum;
My initial solution :-)
2. Print the sum of all even

Fibonacci numbers 

below 4,000,000
(1, 1, * + * ...^ * >
4_000_000).grep(* %% 2).sum.say
3. Find the largest palindromic
number, which is a product

of two three-digit numbers.
(((999...100) X* (999...100)).grep:
{$^a eq $^a.flip}).max.say
3. Find the largest palindromic
number, which is a product

of two three-digit numbers.
(((999...100) X* (999...100)).grep:

{$^a eq $^a.flip}).head(10).max.say
4. Print the sum

of big numbers
4. Print the sum

of big numbers
<
371072874339021027987979982208375902465101357402
# Other 98 numbers here
535035345264725242508740540755917897812643303316
>.sum.substr(0, 10).say
19. Count Sundays between

the two dates
say (
Date.new(year => 1901) ..^ Date.new(year => 2001)
).grep({.day == 1 && .day-of-week == 7}).elems
19. Count Sundays between

the two dates
say (
Date.new(year => 1901, month => 1, day => 1) ..
Date.new(year => 2000, month => 12, day => 31)
).grep(*.day == 1).grep(*.day-of-week == 7).elems
19. Count Sundays between

the two dates
say +(1901..2000 X 1..12).map(
{Date.new(|@_, 1)}
).grep(*.day-of-week == 7);
34. Print the sum of all numbers,
which are equal to the sum
of factorials of their digits
say [+] (3..50_000).grep(

{$_ == [+] .comb.map({[*] 2..$_})})
Perl 6

Golf
Contest8
First prime numbers
say ((1..*).grep: *.is-prime)[10000]
First prime numbers
say ((1..*).grep: *.is-prime)[10000]
.is-prime&&.say for ^Ⅽ
First 30 Fibonacci numbers
.say for (0, 1, * + * ... *)[^31]
First 30 Fibonacci numbers
.say for (0, 1, * + * ... *)[^31]
.say for (0,1,*+*...*)[^31]
First 30 Fibonacci numbers
.say for (0, 1, * + * ... *)[^31]
.say for (0,1,*+*...*)[^31]
(0,1,*+*...*)[^31]>>.say
Side effects!

Not recommended!
First 30 Fibonacci numbers
.say for (0, 1, * + * ... *)[^31]
.say for (0,1,*+*...*)[^31]
(0,1,*+*...*)[^31]>>.say
(0,1,*+*…*)[^31]».say
First 30 Fibonacci numbers
.say for (0, 1, * + * ... *)[^31]
.say for (0,1,*+*...*)[^31]
(0,1,*+*...*)[^31]>>.say
(0,1,*+*…*)[^31]».say
(0,1,*+*…*>514229)».say
First 30 Fibonacci numbers
.say for (0, 1, * + * ... *)[^31]
.say for (0,1,*+*...*)[^31]
(0,1,*+*...*)[^31]>>.say
(0,1,*+*…*)[^31]».say
(0,1,*+*…*>7⁷)».say
First 30 Fibonacci numbers
.say for (0, 1, * + * ... *)[^31]
.say for (0,1,*+*...*)[^31]
(0,1,*+*...*)[^31]>>.say
(0,1,*+*…*)[^31]».say
(0,1,*+*…*>7⁷)».say
(0,1,*+*…*> )».say
First 30 Fibonacci numbers
.say for (0, 1, * + * ... *)[^31]
.say for (0,1,*+*...*)[^31]
(0,1,*+*...*)[^31]>>.say
(0,1,*+*…*)[^31]».say
(0,1,*+*…*>7⁷)».say
(0,1,*+*…*> )».say
(0,1,*+*…/20/)».say
Andrew Shitov


German Perl Workshop

Munich, 7 March 2019
github.com/ash
slideshare.net/andy.sh

Contenu connexe

Tendances

The Joy of Smartmatch
The Joy of SmartmatchThe Joy of Smartmatch
The Joy of Smartmatch
Andrew Shitov
 
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
 
PERL for QA - Important Commands and applications
PERL for QA - Important Commands and applicationsPERL for QA - Important Commands and applications
PERL for QA - Important Commands and applications
Sunil Kumar Gunasekaran
 

Tendances (20)

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
 
The Joy of Smartmatch
The Joy of SmartmatchThe Joy of Smartmatch
The Joy of Smartmatch
 
Data Types Master
Data Types MasterData Types Master
Data Types Master
 
Perl 6 in Context
Perl 6 in ContextPerl 6 in Context
Perl 6 in Context
 
Pop3ck sh
Pop3ck shPop3ck sh
Pop3ck sh
 
PHP PPT FILE
PHP PPT FILEPHP PPT FILE
PHP PPT FILE
 
I, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 OverlordsI, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 Overlords
 
Symfony2 - extending the console component
Symfony2 - extending the console componentSymfony2 - extending the console component
Symfony2 - extending the console component
 
Bag of tricks
Bag of tricksBag of tricks
Bag of tricks
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
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
 
Advanced modulinos trial
Advanced modulinos trialAdvanced modulinos trial
Advanced modulinos trial
 
Introdução ao Perl 6
Introdução ao Perl 6Introdução ao Perl 6
Introdução ao Perl 6
 
Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.
 
PERL for QA - Important Commands and applications
PERL for QA - Important Commands and applicationsPERL for QA - Important Commands and applications
PERL for QA - Important Commands and applications
 
The Magic Of Tie
The Magic Of TieThe Magic Of Tie
The Magic Of Tie
 
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 for Python Developers
PHP for Python DevelopersPHP for Python Developers
PHP for Python Developers
 
PHP Conference Asia 2016
PHP Conference Asia 2016PHP Conference Asia 2016
PHP Conference Asia 2016
 

Similaire à Perl6 one-liners

Pythonlearn-11-Regex.pptx
Pythonlearn-11-Regex.pptxPythonlearn-11-Regex.pptx
Pythonlearn-11-Regex.pptx
Dave Tan
 
関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)
riue
 

Similaire à Perl6 one-liners (20)

Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
 
Swift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdfSwift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdf
 
Elm: give it a try
Elm: give it a tryElm: give it a try
Elm: give it a try
 
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
 
Postgresql 9.3 overview
Postgresql 9.3 overviewPostgresql 9.3 overview
Postgresql 9.3 overview
 
Артём Акуляков - F# for Data Analysis
Артём Акуляков - F# for Data AnalysisАртём Акуляков - F# for Data Analysis
Артём Акуляков - F# for Data Analysis
 
Cheatsheet_Python.pdf
Cheatsheet_Python.pdfCheatsheet_Python.pdf
Cheatsheet_Python.pdf
 
Pythonlearn-11-Regex.pptx
Pythonlearn-11-Regex.pptxPythonlearn-11-Regex.pptx
Pythonlearn-11-Regex.pptx
 
関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)
 
Raspberry Pi - Lecture 5 Python for Raspberry Pi
Raspberry Pi - Lecture 5 Python for Raspberry PiRaspberry Pi - Lecture 5 Python for Raspberry Pi
Raspberry Pi - Lecture 5 Python for Raspberry Pi
 
The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196
 
Functions
FunctionsFunctions
Functions
 
Lập trình Python cơ bản
Lập trình Python cơ bảnLập trình Python cơ bản
Lập trình Python cơ bản
 
TDC2016SP - Trilha Programação Funcional
TDC2016SP - Trilha Programação FuncionalTDC2016SP - Trilha Programação Funcional
TDC2016SP - Trilha Programação Funcional
 
Haskell 101
Haskell 101Haskell 101
Haskell 101
 
beginners_python_cheat_sheet_pcc_all (3).pptx
beginners_python_cheat_sheet_pcc_all (3).pptxbeginners_python_cheat_sheet_pcc_all (3).pptx
beginners_python_cheat_sheet_pcc_all (3).pptx
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with Python
 
Map, Reduce and Filter in Swift
Map, Reduce and Filter in SwiftMap, Reduce and Filter in Swift
Map, Reduce and Filter in Swift
 

Plus de Andrew Shitov

‎Откуда узнать про Perl 6‎
‎Откуда узнать про Perl 6‎‎Откуда узнать про Perl 6‎
‎Откуда узнать про Perl 6‎
Andrew Shitov
 
‎42 £ в ойрах‎
‎42 £ в ойрах‎‎42 £ в ойрах‎
‎42 £ в ойрах‎
Andrew Shitov
 

Plus de Andrew Shitov (20)

Fun with Raspberry PI (and Perl)
Fun with Raspberry PI (and Perl)Fun with Raspberry PI (and Perl)
Fun with Raspberry PI (and Perl)
 
Параллельные вычисления в 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
 
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
 
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
 
How to clean an array
How to clean an arrayHow to clean an array
How to clean an array
 
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
 
Perl 5.10 в 2010-м
Perl 5.10 в 2010-мPerl 5.10 в 2010-м
Perl 5.10 в 2010-м
 
Gearman and Perl
Gearman and PerlGearman and Perl
Gearman and Perl
 
‎Откуда узнать про Perl 6‎
‎Откуда узнать про Perl 6‎‎Откуда узнать про Perl 6‎
‎Откуда узнать про Perl 6‎
 
‎42 £ в ойрах‎
‎42 £ в ойрах‎‎42 £ в ойрах‎
‎42 £ в ойрах‎
 

Dernier

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Dernier (20)

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?
 
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
 
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...
 
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
 
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
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
[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
 
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)
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
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
 
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
 

Perl6 one-liners