Raku By Example
View me onGitHub
## for
.say for ^2020;
.say given 'Wish list';

## when

$_ = 42;
when 42 { say 'find it' };

## for when

for 42, 43, "foo", 44, "bar" {
    when Int       { .say }
    when /:i ^Bar/ { .say }
    default        { say "Not an Int or a Bar" }
}

## for given

for Date.new('2019-01-01') .. Date.new('2019-02-04') {
    printf("%04d%02d%02d\n", .year, .month, .day) given .DateTime
}

## block

my @results = lazy '/usr/share/dict/SOWPODS'.IO.lines.grep: {
    .chars72 &&
    .substr(0, 1) ∈ 'a' .. 'z' &&
    .comb.Bag'A' .. 'Z'
}

## »

my @year = <2019 2018 2012 2020>;
@year».chars;
my @lines = gather "/usr/share/dict/words".IO.lines».chars».take;

## gather take for

gather .take for <Nim Rust Perl>;
gather take 2 * $_ if $_ ** 2 > 19 for ^100;

## 列表解析

gather .take when /7$/ for 1..99;
gather take $_ ** 2 for 1..10;

## with

.say with 42

## {} 

given '20190823' {
    for ((1 .. .chars) ∖ .chars «-» .indices(0)).keys.sort(-*) -> $n {
        say "{.substr(* - $n)} is prime" if .substr(* - $n).is-prime;
    }
}

# 20190823 is prime
# 190823 is prime
# 90823 is prime
# 823 is prime
# 23 is prime
# 3 is prime

## given 块自动设置 topic 变量

given open("output-perl6", :w) {
    my $catFace = "\x1F639";
    .say($catFace);
    .say($catFace.chars);
    my $dWithDots = "D\x0323\x0307";
    .say($dWithDots);
    .say($dWithDots.chars);
    .close;
}