an introduction - geeklan
TRANSCRIPT
Perl6An Introduction
Perl6Raku
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.
● Rakudo
○ Compiler, 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 ParadigmsWhat’s your poison?
Multiple Programming ParadigmsWhat’s your poison?
● Functional Programming ?
Multiple Programming ParadigmsWhat’s your poison?
● Functional Programming ?
● Object Oriented ?
Multiple Programming ParadigmsWhat’s your poison?
● Functional Programming ?
● Object Oriented ?
● Procedural ?
Multiple Programming ParadigmsWhat’s your poison?
● Functional Programming ?
● Object Oriented ?
● Procedural ?
● Event Based ?
Multiple Programming ParadigmsWhat’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;
# If the user has no prefs just return an empty list
# Note calling pref-hash has some overhead...
if ( ! $user.user-has-prefs ) { return (); }
return @preferences.grep( { $user.has-pref($_) } );
}
This is a pretty standard function.
But Raku adds some tools to make
it easier to handle.
Multi Dispatch and Signaturesmulti sub get-user-preferences( Any:U $, @ ) {
die “No User supplied”;
}
multi sub get-user-preferences( User $user,
@prefs where ! * ) {
die “No preferences supplied”;
}
multi sub get-user-preferences( User $user, @preferences ) {
# If the user has no prefs just return an empty list
# Note calling pref-hash has some overhead...
if ( ! $user.user-has-prefs ) { return (); }
return @preferences.grep( { $user.has-pref($_) } );
}
Using multi dispatch we can
remove our boilerplate tests
from the start.
We also can add some type
checking as well.
Multi Dispatch and Signaturesmulti sub get-user-preferences( User:D $user,
@prefs where ! * ) {
die “No preferences supplied”;
}
multi sub get-user-preferences( User:D $user, @preferences ) {
# If the user has no prefs just return an empty list
# Note calling pref-hash has some overhead...
if ( ! $user.user-has-prefs ) { return (); }
return @preferences.grep( { $user.has-pref($_) } );
}
In fact if we specify we want
a defined user then we can
remove one data check.
The system will raise an
Exception as it can’t find a
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 ) {
return @preferences.grep( { $user.has-pref($_) } );
}
We can also take our special
case out into its own multi
sub.
Multi Dispatch and Signaturessubset UserNoPrefs of User where ! *.user-has-prefs;
…multi sub get-user-preferences(
UserNoPrefs $, @ ) {
return ();
}
multi sub get-user-preferences( User $user, @preferences ) {
return @preferences.grep( { $user.has-pref($_) } );
}
Finally we can define a subset
of our User class to make the
code easier to read.
Junctionsmy @array = ( False, False, False );
my ( $all, $none, $any ) = ( all(@array), none(@array), any(@array) );
Junctionsmy @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
Junctionsmy @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
Junctionsmy @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
# one(1,2,3,4,5)
4 < 1^2^3^4^5 < 2 == True;
# any(1,2,3,4,5)
4 < 1|2|3|4|5 < 2 == 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 = “\c[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 $á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;
Á : Á
my $ß = 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
say “$á1 : $á2”.uc;
Á : Á
my $ß = 2;
$ß = ( $ß × ¾ )²;
say “ß => $ß”;
ß => 2.25
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;
Á : Á
my $ß = 2;
$ß = ( $ß × ¾ )²;
say “ß => $ß”;
ß => 2.25
say “ß => $ß”.uc;
SS => 2.25
Sequences, Lazy Evaluation and Rational Numbersmy @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 Bagsmy @primes = (1..*).grep( *.is-prime );
my @fib = 1,2,*+*...*;
my $prime-set = set( @primes[0..50] );
say $_, " prime? ", $_ ∈ $prime-set
for @fib[0..5];
say $prime-set ∩ @fib[0..10];
(elem) and ∈ are synonyms as are (&) and ∩
Note : Set operators auto coerce their args to Sets.
1 prime? False
2 prime? True
3 prime? True
5 prime? True
8 prime? False
13 prime? True
set(13 2 3 5 89)
Native Call Interface to external librariesuse 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;
}
}
};
.write_png("test2.png")
}
https://github.com/timo/cairo-p6
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 Perl 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
● Learning Perl 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?