an introduction - geeklan

55
Perl6 An Introduction

Upload: others

Post on 13-Jan-2022

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: An Introduction - GeekLAN

Perl6An Introduction

Page 2: An Introduction - GeekLAN

Perl6Raku

An Introduction

Page 3: An Introduction - GeekLAN

The nuts and bolts● Spec tests

○ Complete test suite for the language.

○ Anything that passes the suite is Raku.

Page 4: An Introduction - GeekLAN

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)

Page 5: An Introduction - GeekLAN

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

Page 6: An Introduction - GeekLAN

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.

Page 7: An Introduction - GeekLAN

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

Page 8: An Introduction - GeekLAN

Multiple Programming ParadigmsWhat’s your poison?

Page 9: An Introduction - GeekLAN

Multiple Programming ParadigmsWhat’s your poison?

● Functional Programming ?

Page 10: An Introduction - GeekLAN

Multiple Programming ParadigmsWhat’s your poison?

● Functional Programming ?

● Object Oriented ?

Page 11: An Introduction - GeekLAN

Multiple Programming ParadigmsWhat’s your poison?

● Functional Programming ?

● Object Oriented ?

● Procedural ?

Page 12: An Introduction - GeekLAN

Multiple Programming ParadigmsWhat’s your poison?

● Functional Programming ?

● Object Oriented ?

● Procedural ?

● Event Based ?

Page 13: An Introduction - GeekLAN

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.

Page 14: An Introduction - GeekLAN

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

Page 15: An Introduction - GeekLAN

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

1

Page 16: An Introduction - GeekLAN

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

1

0.1 + 0.2 - 0.3 == 0

Page 17: An Introduction - GeekLAN

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

1

0.1 + 0.2 - 0.3 == 0

1 < 3 > 2

Page 18: An Introduction - GeekLAN

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

1

0.1 + 0.2 - 0.3 == 0

1 < 3 > 2

٤٥ == 45 == �� == ४५

Page 19: An Introduction - GeekLAN

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

Page 20: An Introduction - GeekLAN

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

}

Page 21: An Introduction - GeekLAN

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.

Page 22: An Introduction - GeekLAN

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.

Page 23: An Introduction - GeekLAN

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.

Page 24: An Introduction - GeekLAN

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.

Page 25: An Introduction - GeekLAN

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.

Page 26: An Introduction - GeekLAN

Junctionsmy @array = ( False, False, False );

my ( $all, $none, $any ) = ( all(@array), none(@array), any(@array) );

Page 27: An Introduction - GeekLAN

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

Page 28: An Introduction - GeekLAN

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

Page 29: An Introduction - GeekLAN

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;

Page 30: An Introduction - GeekLAN

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

Page 31: An Introduction - GeekLAN

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

my $p1 = start { sleep 3; print “Or a simple start? ”; };

Page 32: An Introduction - GeekLAN

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? ” } );

Page 33: An Introduction - GeekLAN

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. ” } );

Page 34: An Introduction - GeekLAN

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.” } );

Page 35: An Introduction - GeekLAN

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.”;

Page 36: An Introduction - GeekLAN

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…

Page 37: An Introduction - GeekLAN

Channels and Supplies

Page 38: An Introduction - GeekLAN

Channels and Supplies● Channels allow for FIFO messaging between threads

Page 39: An Introduction - GeekLAN

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

Page 40: An Introduction - GeekLAN

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

Page 41: An Introduction - GeekLAN

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

Page 42: An Introduction - GeekLAN

Unicode# Single Character

my $á1 = “\c[LATIN SMALL LETTER A WITH ACUTE]”;

my $á2 = “a\x301”; # Combining Acute

Page 43: An Introduction - GeekLAN

Unicode# Single Character

my $á1 = “\c[LATIN SMALL LETTER A WITH ACUTE]”;

my $á2 = “a\x301”; # Combining Acute

say “$á1 : $á2”;

á : á

Page 44: An Introduction - GeekLAN

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

Page 45: An Introduction - GeekLAN

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;

Á : Á

Page 46: An Introduction - GeekLAN

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;

$ß = ( $ß × ¾ )²;

Page 47: An Introduction - GeekLAN

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

Page 48: An Introduction - GeekLAN

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

Page 49: An Introduction - GeekLAN

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)

Page 50: An Introduction - GeekLAN

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)

Page 51: An Introduction - GeekLAN

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

Page 52: An Introduction - GeekLAN

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.

Page 53: An Introduction - GeekLAN

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)

Page 54: An Introduction - GeekLAN

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

Page 55: An Introduction - GeekLAN

Questions?