An Introduction to Raku

An Introduction to Raku

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. ● 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 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 make # 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

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    55 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us