Perl6

An Introduction Perl6 Raku

An Introduction The nuts and bolts

● Spec tests ○ Complete test suite for the language. ○ Anything that passes the suite is Raku. The nuts and bolts

● Spec tests ○ Complete test suite for the language. ○ Anything that passes the suite is Raku. ● , compiles Raku to be run on a number of target VM’s (92% written in Raku) The nuts and bolts

● Spec tests ○ Complete test suite for the language. ○ Anything that passes the suite is Raku. ● Rakudo ○ Compiler, compiles Raku to be run on a number of target VM’s (92% written in Raku) ● MoarVM ○ Short for "Metamodel On A Runtime" ○ Threaded, garbage collected VM optimised for Raku The nuts and bolts

● Spec tests ○ Complete test suite for the language. ○ Anything that passes the suite is Raku. ● Rakudo ○ Compiler, compiles Raku to be run on a number of target VM’s (92% written in Raku) ● MoarVM ○ Short for "Metamodel On A Runtime" ○ Threaded, garbage collected VM optimised for Raku ● JVM ○ The Java Virtual machine. The nuts and bolts

● Spec tests ○ Complete test suite for the language. ○ Anything that passes the suite is Raku. ● Rakudo ○ Compiler, compiles Raku to be run on a number of target VM’s (92% written in Raku) ● MoarVM ○ Short for "Metamodel On A Runtime" ○ Threaded, garbage collected VM optimised for Raku ● JVM ○ The Java Virtual machine. ● Rakudo JS (Experimental) ○ Compiles your Raku to a Javascript file that can run in a browser Multiple Programming Paradigms

What’s your poison? Multiple Programming Paradigms

What’s your poison?

● Functional Programming ? Multiple Programming Paradigms

What’s your poison?

● Functional Programming ? ● Object Oriented ? Multiple Programming Paradigms

What’s your poison?

● Functional Programming ? ● Object Oriented ? ● Procedural ? Multiple Programming Paradigms

What’s your poison?

● Functional Programming ? ● Object Oriented ? ● Procedural ? ● Event Based ? Multiple Programming Paradigms

What’s your poison?

● Functional Programming ? ● Object Oriented ? ● Procedural ? ● Event Based ?

Raku builds in the concept of the swiss army chainsaw and gives you a toolbox from which you can build whatever chainsaw you want. What is truth?

In Raku all of these statements are True: What is truth?

In Raku all of these statements are True:

1 What is truth?

In Raku all of these statements are True:

1

0.1 + 0.2 - 0.3 == 0 What is truth?

In Raku all of these statements are True:

1

0.1 + 0.2 - 0.3 == 0

1 < 3 > 2 What is truth?

In Raku all of these statements are True:

1

0.1 + 0.2 - 0.3 == 0

1 < 3 > 2

४५ == == 45 == ٤٥ What is truth?

In Raku all of these statements are True:

1

0.1 + 0.2 - 0.3 == 0

1 < 3 > 2

४५ == == 45 == ٤٥

0 but True Objects, Types and SubSets

● By default Objects are Immutable ○ Useful for both Functional Paradigms and Thread safety ● Both Inheritance and Composition are available and can be mixed ● Roles used for Composition also can be used as Interfaces ● Subsets allow for simple runtime sub type checking

subset SmallInt of Int where * < 50;

sub foo( SmallInt $num ) { say “*” x $num } Multi Dispatch and Signatures

#| Given a user object and a list of preference #| Return the list of preferences that user has. sub get-user-preferences( $user, @preferences ) { die “No User supplied” unless $user:defined; die “No preferences supplied” unless @preferences; This is a pretty standard function. # If the user has no prefs just return an empty list But Raku adds some tools to # Note calling pref-hash has some overhead... it easier to handle. if ( ! $user.user-has-prefs ) { return (); }

return @preferences.grep( { $user.has-pref($_) } ); } Multi Dispatch and Signatures multi sub get-user-preferences( Any:U $, @ ) { die “No User supplied”; } multi sub get-user-preferences( User $user, @prefs where ! * ) { Using multi dispatch we can die “No preferences supplied”; remove our boilerplate tests } from the start. multi sub get-user-preferences( User $user, @preferences ) { We also can add some type # If the user has no prefs just return an empty list checking as well. # Note calling pref-hash has some overhead... if ( ! $user.user-has-prefs ) { return (); }

return @preferences.grep( { $user.has-pref($_) } ); } Multi Dispatch and Signatures multi sub get-user-preferences( User:D $user, @prefs where ! * ) { die “No preferences supplied”; } In fact if we specify we want multi sub get-user-preferences( User:D $user, @preferences ) { a defined user then we can # If the user has no prefs just return an empty list remove one data check. # Note calling pref-hash has some overhead... if ( ! $user.user-has-prefs ) { return (); } The system will raise an Exception as it can’t find a return @preferences.grep( { $user.has-pref($_) } ); matching sub to use. } Multi Dispatch and Signatures

… multi sub get-user-preferences( User:D $user where ! *.user-has-prefs, @ ) { return (); } multi sub get-user-preferences( User:D $user, @preferences ) { We can also take our special return @preferences.grep( { $user.has-pref($_) } ); case out into its own multi } sub. Multi Dispatch and Signatures subset UserNoPrefs of User where ! *.user-has-prefs; … multi sub get-user-preferences( UserNoPrefs $, @ ) { return (); } Finally we can define a subset multi sub get-user-preferences( User $user, @preferences ) { of our User class to make the return @preferences.grep( { $user.has-pref($_) } ); code easier to read. } Junctions my @array = ( False, False, False ); my ( $all, $none, $any ) = ( all(@array), none(@array), any(@array) ); Junctions my @array = ( False, False, False ); my ( $all, $none, $any ) = ( all(@array), none(@array), any(@array) ); say "All : {$all.so} None: {$none.so} Any: {$any.so}";

All: False None: True Any: False Junctions my @array = ( False, False, False ); my ( $all, $none, $any ) = ( all(@array), none(@array), any(@array) ); say "All : {$all.so} None: {$none.so} Any: {$any.so}";

All: False None: True Any: False

@array[0] = True; say "All : {$all.so} None: {$none.so} Any: {$any.so}";

All: False None: False Any: True Junctions my @array = ( False, False, False ); my ( $all, $none, $any ) = ( all(@array), none(@array), any(@array) ); say "All : {$all.so} None: {$none.so} Any: {$any.so}";

All: False None: True Any: False # one(1,2,3,4,5) @array[0] = True; 4 < 1^2^3^4^5 < 2 == True; say "All : {$all.so} None: {$none.so} Any: {$any.so}"; # any(1,2,3,4,5) 4 < 1|2|3|4|5 < 2 == True; All: False None: False Any: True # all(1,2,3,4,5) 4 < 1&2&3&4&5 < 2 == False; Promises

“Parallel programming for mortals” or “Basically just like Node” Promises

“Parallel programming for mortals” or “Basically just like Node” my $p1 = start { sleep 3; print “Or a simple start? ”; }; Promises

“Parallel programming for mortals” or “Basically just like Node” my $p1 = start { sleep 3; print “Or a simple start? ”; }; my $p2 = Promise.in(2).then( { print “Why not use a timer? ” } ); Promises

“Parallel programming for mortals” or “Basically just like Node” my $p1 = start { sleep 3; print “Or a simple start? ”; }; my $p2 = Promise.in(2).then( { print “Why not use a timer? ” } ); my $p3 = Promise.anyof( $p1, $p2 ).then( { print “Something is done. ” } ); Promises

“Parallel programming for mortals” or “Basically just like Node” my $p1 = start { sleep 3; print “Or a simple start? ”; }; my $p2 = Promise.in(2).then( { print “Why not use a timer? ” } ); my $p3 = Promise.anyof( $p1, $p2 ).then( { print “Something is done. ” } ); my $p4 = Promise.allof( $p1, $p2, $p3 ).then( { print “All the promises done.” } ); Promises

“Parallel programming for mortals” or “Basically just like Node” my $p1 = start { sleep 3; print “Or a simple start? ”; }; my $p2 = Promise.in(2).then( { print “Why not use a timer? ” } ); my $p3 = Promise.anyof( $p1, $p2 ).then( { print “Something is done. ” } ); my $p4 = Promise.allof( $p1, $p2, $p3 ).then( { print “All the promises done.” } ); print “Promises Begun… ”; await( $p1, $p2, $p3, $p4 ); say “All done.”; Promises

“Parallel programming for mortals” or “Basically just like Node” my $p1 = start { sleep 3; print “Or a simple start? ”; }; my $p2 = Promise.in(2).then( { print “Why not use a timer? ” } ); my $p3 = Promise.anyof( $p1, $p2 ).then( { print “Something is done. ” } ); my $p4 = Promise.allof( $p1, $p2, $p3 ).then( { print “All the promises done.” } ); print “Promises Begun… ”; await( $p1, $p2, $p3, $p4 ); say “All done.”;

Promises Begun… Why not use a timer? Something is done. Or a simple start? All the promises done. All done.

With some pauses… Channels and Supplies Channels and Supplies

● Channels allow for FIFO messaging between threads Channels and Supplies

● Channels allow for FIFO messaging between threads ○ Channels can also be treated as lists with data filtering and manipulation being done on the fly ○ Easily fits into a message based data processing paradigm Channels and Supplies

● Channels allow for FIFO messaging between threads ○ Channels can also be treated as lists with data filtering and manipulation being done on the fly ○ Easily fits into a message based data processing paradigm ● Supplies give event driven responsive messaging Channels and Supplies

● Channels allow for FIFO messaging between threads ○ Channels can also be treated as lists with data filtering and manipulation being done on the fly ○ Easily fits into a message based data processing paradigm ● Supplies give event driven responsive messaging ○ react / whenever blocks allow for simple handling of events ○ Cro microservice framework built on the concept of chains of supplies from request through layers of middleware and data processing to response Unicode

# Single Character my $á1 = “\[LATIN SMALL LETTER A WITH ACUTE]”; my $á2 = “a\x301”; # Combining Acute Unicode

# Single Character my $á1 = “\c[LATIN SMALL LETTER A WITH ACUTE]”; my $á2 = “a\x301”; # Combining Acute say “$á1 : $á2”; á : á Unicode

# Single Character my $á1 = “\c[LATIN SMALL LETTER A WITH ACUTE]”; my $á2 = “a\x301”; # Combining Acute say “$á1 : $á2”; á : á say $á1 ~~ á2; True Unicode

# Single Character my $á1 = “\c[LATIN SMALL LETTER A WITH ACUTE]”; my $á2 = “a\x301”; # Combining Acute say “$á1 : $á2”; á : á say $á1 ~~ á2; True say “$á1 : $á2”.uc; Á : Á Unicode

# Single Character my $ß = 2; my $á1 = “\c[LATIN SMALL LETTER A WITH ACUTE]”; $ß = ( $ß × ¾ )²; my $á2 = “a\x301”; # Combining Acute say “$á1 : $á2”; á : á say $á1 ~~ á2; True say “$á1 : $á2”.uc; Á : Á Unicode

# Single Character my $ß = 2; my $á1 = “\c[LATIN SMALL LETTER A WITH ACUTE]”; $ß = ( $ß × ¾ )²; my $á2 = “a\x301”; # Combining Acute say “ß => $ß”; say “$á1 : $á2”; ß => 2.25 á : á say $á1 ~~ á2; True say “$á1 : $á2”.uc; Á : Á Unicode

# Single Character my $ß = 2; my $á1 = “\c[LATIN SMALL LETTER A WITH ACUTE]”; $ß = ( $ß × ¾ )²; my $á2 = “a\x301”; # Combining Acute say “$á1 : $á2”; say “ß => $ß”; á : á ß => 2.25 say $á1 ~~ á2; True say “ß => $ß”.uc; say “$á1 : $á2”.uc; SS => 2.25 Á : Á Sequences, Lazy Evaluation and Rational Numbers my @primes = (1..*).grep( *.is-prime ); my @evens = 2,4,6...*; my @fib = 1, 1, * + * ... *;

my $div0 = 42 / 0; say $div0.nude; # NU(merator and) DE(nominator) (42 0) Sets and Bags my @primes = (1..*).grep( *.is-prime ); 1 prime? False my @fib = 1,2,*+*...*; 2 prime? True 3 prime? True my $prime-set = set( @primes[0..50] ); 5 prime? True say $_, " prime? ", $_ ∈ $prime-set 8 prime? False for @fib[0..5]; 13 prime? True set(13 2 3 5 89) say $prime-set ∩ @fib[0..10];

(elem) and ∈ are synonyms as are (&) and ∩

Note : Set operators auto coerce their args to Sets. Native Call Interface to external libraries use Cairo; given Cairo::Image.create(Cairo::FORMAT_ARGB32, 128, 128) { given Cairo::Context.new($_) { for 1..16 -> $x { for 1..16 -> $y { .rgb($x/16, $y/16, 0 ); .rectangle( 8 * ( $x - 1), 8 * ( $y - 1 ), 8 , 8 ); .fill; } } }; https://github.com/timo/cairo-p6 .write_png("test2.png") } NativeCall (A peek inside)

method write_to_png(Str $filename) returns int32 is native($cairolib) is symbol('cairo_surface_write_to_png') {*}

method rectangle(num64 $x, num64 $y, num64 $w, num64 $h) is native($cairolib) is symbol('cairo_rectangle') {*}

That simple. Here $cairolib is either 'libcairo-2' or ('cairo', v2) depending on the architecture. All the other stuff

● Grammars ● Imaginary Numbers ● Proper Exceptions ● CPAN ● Meta Objects ● Telemetry ● IO::Notification ● Date and DateTime built in ● 317 built in types in fact... ● (And so much more) Further Reading (and Viewing)

● Raku Docs - https://docs.raku.org/ ● High End Unicode in 6 - https://youtu.be/Oj_lgf7A2LM ● Perl6 Superglue for the 21st Century - https://www.youtube.com/watch?v=q8stPrG1rDo ● Think Perl 6 - http://greenteapress.com/wp/think-perl-6/ ● Using Perl 6 - https://deeptext.media/using-perl6 ● 6 - https://www.learningperl6.com/ ● Cro - http://cro.services/ ● Sparrowdo - https://github.com/melezhik/sparrowdo ● Roles vs Inheritance - https://www.youtube.com/watch?v=cjoWu4eq1Tw ● Perl6 Concurrency - https://www.youtube.com/watch?v=hGyzsviI48M Questions?