A few patterns

And one antipattern

You might see only in Raku

jjmerelo, Raku core dev

Raku: The language for the next 100 years

Multiparadigm language

Object oriented & functional

Asynchronous & concurrent

Full Unicode support + Grammars

A taste of Raku

my $digits-of-ᴨ = Channel.new;
my &sequence-gap-four = { $^α, $^α+4 … ∞ };
my @σ-plus = sequence-gap-four( 5 );
my $one = start {
    while my $σ = shift @σ-plus { $digits-of-ᴨ.send( 1.0 / $σ ) };
}
my @σ-minus = sequence-gap-four( 3 );
my $two = start {
    while my $σ = shift @σ-minus { $digits-of-ᴨ.send( -1.0 / $σ ) };
}
my $ᴨ-so-far = 1;
my $p = start react {
    whenever $digits-of-ᴨ -> $ε {
        atomic-assign( $ᴨ-so-far, atomic-fetch( $ᴨ-so-far ) + $ε);
        say $ᴨ-so-far*4;
    }
}
await $p, $one, $two;

Let's Raku & Role

Roles/mixins contain composable attributes, methods or both

Let's mix in some roles

say "$_ → " ~ $_.Bool for (^10).map: { $_ but so $_ %% 2 };
Roling even or odd

And give them some colors

use Colorizable;
for ^10 -> $κ {
    my $ψ = ($κ but uniname(~$κ)) but Colorizable;
    (given $ψ mod 3 {
        when 0 { $ψ.colorize(:fg(red)) }
        when 1 { $ψ.colorize(:fg(yellow)) }
        when 2 { $ψ.colorize(:fg(blue)) }
    }).say
};

Show the true colors

Colored names

🗱①: Type-safe tunnelling

sub priming( UInt $num where * --> Array[UInt] ) {
    my @these-primes = (^($num/2)).grep: *.is-prime;
    my UInt @factors = @these-primes.grep: !( $num mod * );
    if ( (1,|@factors).sum == $num ) {
        return @factors but "👑";
    } elsif !@factors {
        return @factors but "🍅";
    } else {
        return @factors;
    }
}

for ^1000 -> $n {
    my $result = priming($n);
    say $n, ", ", ~$result;
}

Some light at the end

Grammars are to regexes what classes are to routines

From Raku Recipes

grammar Measurements {
    token TOP      { <quantity> \h* <unit>? }
    token quantity { <:N>+ }
    token unit     { ["g" | "tbsp" | "clove" | "cup" ] s? }
}

for "3 tbsps", "½ cup", "𐄩 cloves" -> $m {
    say Measurements.parse( $m );
}

Getting the ingredients

「3 tbsps」
 quantity => 「3」
 unit => 「tbsps」
「½ cup」
 quantity => 「½」
 unit => 「cup」
「𐄩 cloves」
 quantity => 「𐄩」
 unit => 「cloves」

🗱②: Grammaroles: Roles that can be composed into grammars.

my @unit-types=<g tbsp clove cup sp pinch>;
my @products=<lentils apple egg garlic>;
role Measures {
    token quantity { <:N>+ }
    token unit     { @unit-types s? }
}
grammar Measured-Ingredients does Measures {
    token TOP { <measured-ingredient> }
    token measured-ingredient {
      [ <quantity> \h* <unit> \h+ <product> || <quantity> \h+ <product>] s?
    }
    token product {:i @products }
}
for "1 cup lentils", "½ clove garlic", "2 eggs" -> $m {
    say Measured-Ingredients.parse( $m );
}

Antipattern: using classic for indexed loops

(Don't) Loop

sub factors( $num ) {
    my @these-primes = (^($num/2)).grep: *.is-prime;
    print "\nFactors for $num →";
    loop ( my $i = 1; $i < @these-primes.elems; $i++ ) {
        (@these-primes[$i]~" ").print unless $num mod @these-primes[$i];
    }
}

factors( $_ )  for 2..5000;

Do

constant MAX-NUM = 5_000;
my @these-primes = (^(MAX-NUM/2)).grep: *.is-prime;
sub is-mod( $n, $b ) { ! ($n mod $b ) } 
my @modders = @these-primes.map: -> $b { ( -> $c { is-mod( $c, $b ) } ) but $b };

(2..MAX-NUM).race.map: -> $n {
    say $n, " ",
      @modders.grep( *.Int <= $n/2)
              .grep( { $_($n) } )
              .map( *.Int ~ " " )
              .join( ", " )
     || '🍅';
;}

Showing the primes

Multiple paradigms

Multiple patterns

But TIMTOWDI

Give Raku a chance