Raku Sheet

Raku 札记

正则表达式

Raku 的正则表达式允许你为你自定义的字符类混进 Unicode 属性!

constant string = 'The Quick 🐼 Jumped Over 57 Dogs';

say string ~~ m:g/ <:upper-[TQ]>                /; # (「J」 「O」 「P」)
say string ~~ m:g/ <-[\x0..\x255]+[\x35..\x37]> /; # (「🐼」 「5」 「7」)
say string ~~ m:g/ <-:digit -:Ll -:Zs>          /; # (「T」 「Q」 「🐼」 「J」 「O」 「D」)
say string ~~ m:g/ <[\w]-[TQJ\d]-:lower+[5]>    /; # (「O」 「5」 「D」)

Wow! #Raku #Regex is so awesome, you can just shove an array into it and it’ll know to treat it as alternatives to match!

my @stuff = <fo ba me>;
say 'foo bar meows' ~~ m:g/ @stuff        \S+ /; # OUTPUT: (「foo」 「bar」 「meows」)
say 'foo bar meows' ~~ m:g/ @(<fo ba me>) \S+ /; # OUTPUT: (「foo」 「bar」 「meows」)

# Defaults to Longest Token Match:
say 'meowy' ~~ /   @(<me meow>) /; # OUTPUT: 「meow」

# Explicit Longest Token Match; just add a pipe char before the Array
say 'meowy' ~~ /  |@(<me meow>) /; # OUTPUT: 「meow」

# First Token Match; just add a two pipes before the Array
say 'meowy' ~~ / ||@(<me meow>) /; # OUTPUT: 「me」

# The pipe chars are the same stuff you'd use if you wrote the array out:
# Longest Token Match:
say 'meowy' ~~ / 'me' |  'meow' /; # OUTPUT: 「meow」
# First Token Match:
say 'meowy' ~~ / 'me' || 'meow' /; # OUTPUT: 「me」
D:\blog\ohmysummer>raku -e "my @releases=map {sprintf '%d-%02d', .year, .month},  (Date.new(:2016year :12month), *.later(:month) ... Date.new: :2017year, :4month ); for @releases.map(*.split('-').join: '.').rotor: 2 => -1  { print qqx/printf '%s - %-7s:' { .[0] } { .[1] };  git log --oneline '{.[0]} ... {.[1]}' | wc -l /  };"

END {
    say "Doing some clean up here or something...";
    say 'Good bye!';
}

for ^1234 .grep: *.is-prime -> $x {
    FIRST say "First prime is $x";
    LAST  say  "Last prime is $x";
}

sleep 3;
say "This script ran for {now - INIT now} seconds";

# OUTPUT:
# First prime is 2
# Last prime is 1231
# This script ran for 13.0272261 seconds
# Doing some clean up here or something...
# Good bye!
Raku 

comments powered by Disqus