Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Perl6 one-liners

6,965 views

Published on

During the talk, I will show a number of short Perl 6 fragments (mostly one-liners), that can express complex problems in a very concise way.

We will also solve a few problems from Project Euler, where Perl 6 can demonstrate its extreme beauty.

Published in: Technology
  • Be the first to comment

Perl6 one-liners

  1. 1. Perl 6 One-Liners Andrew Shitov 
 German Perl Workshop
 Munich, 8 March 2019
  2. 2. A PLAY IN EIGHT ACTS
  3. 3. Rakudo command-line options1
  4. 4. -e execute
  5. 5. $ perl6 -e'say 42'
  6. 6. $ perl6 -e'$*PERL.version.say' v6.d
  7. 7. -e, not -E
  8. 8. -n repeat for each input line
  9. 9. $ perl6 -ne'say [+] .split(" ")' data.txt
  10. 10. -p repeat and print
  11. 11. $ perl6 -npe'.=flip' data.txt
  12. 12. Perl 6 am MAIN 2
  13. 13. 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" }
  14. 14. $ perl6 main.pl No args $ perl6 main.pl 1 Single arg 1 $ perl6 main.pl 1 2 Two args: 1 and 2
  15. 15. $ perl6 main.pl 1 2 3 Usage: main.pl <x> <y>
  16. 16. multi sub MAIN(Int $x) { say "Integer $x" } multi sub MAIN(Str $s) { say "String $s" }
  17. 17. $ perl6 main2.pl abc String abc
  18. 18. $ 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
  19. 19. multi sub MAIN(IntStr $x) { say "Integer $x" } multi sub MAIN(Str $s) { say "String $s" }
  20. 20. multi sub MAIN(*@args) { say "Passed {@args.elems} args" }
  21. 21. $ perl6 main3.pl Passed 0 args $ perl6 main3.pl 1 Passed 1 args $ perl6 main3.pl a b c d Passed 4 args
  22. 22. Mangling
 Text
 Files3
  23. 23. 8 exercises
  24. 24. $ perl6 -npe's/$/n/' text.txt
  25. 25. $ perl6 -npe's/$/n/' text.txt Double-space a file
  26. 26. $ perl6 -ne'.say if .chars' text.txt
  27. 27. $ perl6 -ne'.say if .chars' text.txt Remove all blank lines
  28. 28. $ perl6 -ne'.say if /S/' text.txt
  29. 29. $ perl6 -ne'.say if /S/' text.txt Remove all blank lines
  30. 30. docs.perl6.org/language/variables#The_$_variable
  31. 31. $ perl6 -ne'say ++$ ~ ". " ~ $_' text.txt
  32. 32. Number all lines in a file $ perl6 -ne'say ++$ ~ ". " ~ $_' text.txt
  33. 33. $ perl6 -npe'.=uc' text.txt
  34. 34. Convert all text to uppercase $ perl6 -npe'.=uc' text.txt
  35. 35. $ perl6 -npe'.=trim' text.txt
  36. 36. Strip whitespaces $ perl6 -npe'.=trim' text.txt
  37. 37. $ perl6 -ne'.say ; exit' text.txt
  38. 38. Print the first line of a file $ perl6 -ne'.say ; exit' text.txt
  39. 39. $ perl6 -npe'exit if $++ == 10' text.txt
  40. 40. Print the first 10 lines of a file $ perl6 -npe'exit if $++ == 10' text.txt
  41. 41. Reverse a file .say for lines.reverse $ perl6 reverse.pl < text.txt
  42. 42. Reverse a file .say for $*IN.lines.reverse $ perl6 reverse.pl < text.txt
  43. 43. Reverse a file .say for @*ARGS[0].IO.open.lines.reverse $ perl6 reverse.pl text.txt
  44. 44. Reading from multiple files .say for $*ARGFILES.lines $ perl6 work.pl a.txt b.txt
  45. 45. Batch file renaming @*ARGS[0..*-2].sort.map: *.Str.IO.rename(++@*ARGS[*-1]) $ perl6 rename.pl *.jpg img_0000.jpg
  46. 46. Perl 6
 Meta- Operators4
  47. 47. Compute totals put [Z+] lines.map: *.words
  48. 48. Merge into columns .say for [Z~] @*ARGS.map: *.IO.lines; $ perl6 merge.pl a.txt b.txt
  49. 49. 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)
  50. 50. Factorial say [*] 1..2019
  51. 51. Least common multiplier say [lcm] 1..20
  52. 52. Rotate a matrix [Z] <A B C>, <D E F>, <H I J> ((A D H) (B E I) (C F J))
  53. 53. Sequence
 Operator
 . . .5
  54. 54. Fibonacci numbers 0, 1, * + * ... *
  55. 55. Fibonacci numbers .say for (0, 1, * + * ... *)[^31] First 30 elements
  56. 56. Prime numbers say ((1..*).grep: *.is-prime)[10000] The 10001 prime numberst
  57. 57. Prime numbers .say if .is-prime for ^100 Prime numbers below 100
  58. 58. The value of π say π
  59. 59. The value of π say pi
  60. 60. The value of π say 4 * [+] (^1000).map({(-1) ** $_ / (2 * $_ + 1)})
  61. 61. The value of π say 4 * [+] (1, -1, 1 … *) «/« 
 (1, 3, 5 … 9999);
  62. 62. Area of a square say π × $𝜌²
  63. 63. Generating
 Random
 Numbers6
  64. 64. Random password ('0'..'z').pick(15).join.say Non-repeating characters
  65. 65. Random password ('0'..'z').roll(15).join.say Characters may repeat
  66. 66. ('0'..'z').join.say 0123456789:;<=>?@ ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_` abcdefghijklmnopqrstuvwxyz
  67. 67. Random integer 2019.rand.Int.say 0 to 2019
  68. 68. Random integer 2019.rand.rand.Int.say Is this number more random? :-)
  69. 69. Solving Euler Problems7
  70. 70. 1. Print the sum of all multiples
 of 3 and 5 below 1000 say sum((1..999).grep: * %% (3 | 5))
  71. 71. 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 :-)
  72. 72. 2. Print the sum of all even
 Fibonacci numbers 
 below 4,000,000 (1, 1, * + * ...^ * > 4_000_000).grep(* %% 2).sum.say
  73. 73. 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
  74. 74. 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
  75. 75. 4. Print the sum
 of big numbers
  76. 76. 4. Print the sum
 of big numbers < 371072874339021027987979982208375902465101357402 # Other 98 numbers here 535035345264725242508740540755917897812643303316 >.sum.substr(0, 10).say
  77. 77. 19. Count Sundays between
 the two dates say ( Date.new(year => 1901) ..^ Date.new(year => 2001) ).grep({.day == 1 && .day-of-week == 7}).elems
  78. 78. 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
  79. 79. 19. Count Sundays between
 the two dates say +(1901..2000 X 1..12).map( {Date.new(|@_, 1)} ).grep(*.day-of-week == 7);
  80. 80. 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..$_})})
  81. 81. Perl 6
 Golf Contest8
  82. 82. First prime numbers say ((1..*).grep: *.is-prime)[10000]
  83. 83. First prime numbers say ((1..*).grep: *.is-prime)[10000] .is-prime&&.say for ^Ⅽ
  84. 84. First 30 Fibonacci numbers .say for (0, 1, * + * ... *)[^31]
  85. 85. First 30 Fibonacci numbers .say for (0, 1, * + * ... *)[^31] .say for (0,1,*+*...*)[^31]
  86. 86. First 30 Fibonacci numbers .say for (0, 1, * + * ... *)[^31] .say for (0,1,*+*...*)[^31] (0,1,*+*...*)[^31]>>.say Side effects!
 Not recommended!
  87. 87. First 30 Fibonacci numbers .say for (0, 1, * + * ... *)[^31] .say for (0,1,*+*...*)[^31] (0,1,*+*...*)[^31]>>.say (0,1,*+*…*)[^31]».say
  88. 88. 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
  89. 89. 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
  90. 90. 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
  91. 91. 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
  92. 92. Andrew Shitov 
 German Perl Workshop
 Munich, 7 March 2019 github.com/ash slideshare.net/andy.sh

×