my @a=1,2,3;
my $s='Escape Plan';
my %h='Rakudo'=>'Star','STD'=>'Larry';
my $capture = \(@a,$s,%h);
say(|$capture).perl;
sub greet($name, :$greeting = 'Hi') {
say "$greeting, $name!";
}
greet('Лена', greeting => 'Привет');
use MONKEY-SEE-NO-EVAL;
multi sub MAIN(Str :r(:$regex), Str :s(:$substr), Str :e(:$ext) = '.out', Int :i(:$ignore-line) = 0, *@files) {
for @files -> $file {
my $out = open $file ~ ".out", :w;
for $file.IO.lines.kv -> $index, $line is copy {
next if $index <= $ignore-line;
$line ~~ EVAL "s/" ~ $regex ~ "/" ~ $substr ~ "/";
say $/;
$out.say($line);
}
$out.close;
}
}
sub rectangle(:$width!, :$height!, :$char = 'X') {
say $char x $width for ^$height;
}
rectangle char => 'o', width => 8, height => 4;
rectangle :width(20), :height<5>;
multi limit-lines (Str $s, Int:D $limit) {
my @lines = $s.lines;
@lines[0 ..^ min @lines.elems, $limit].join("\n");
}
multi limit-lines (Str $s, Int:U $) {$s}
say (limit-lines "a \n b \n c \n d \n", 3).perl;
say limit-lines "a \n b \n c", Int;
sub f(::T $p1, T $p2, ::C) {
my C $closure = $p1 / $p2;
return sub (T $p1) {
$closure * $p1;
}
}
my &s = f(10,2, Int.new / Int.new);
say s(2);
say 'x' x 55;
multi ff(:$named) { note &?ROUTINE.signature }
multi ff(:$also-named) { note &?ROUTINE.signature }
for 'named', 'also-named' -> $n {
ff(|($n => rand));
}
multi sub total(Array @distances) {
}
multi sub total(Int @distances) {
}
sub discount($price, $percent
where (1 <= $percent <= 100)) {
say "You get $percent% off! Pay EUR " ~ $price - ($price * $percent / 100);
}
discount(100, 20);
discount(100, 200);
constant term:<ANSI-SAME-LINE> = "\e[1F\e[0K";
subset Seconds of Numeric;
my regex number { \d+ [ '.' \d+ ]? }
my %unit-multipliers = 'd' => 60*60*24, 'h' => 60*60, 'm' => 60, 's' => 1;
multi sub pretty-print(Seconds $seconds is copy --> Str) {
my @ret;
for %unit-multipliers.sort(-*.value) -> (:key($unit), :value($multiplier)) {
@ret.push: $seconds.Int div $multiplier ~ $unit if $seconds.Int div $multiplier;
$seconds = $seconds % $multiplier;
}
@ret.join: ' ';
}
multi sub MAIN(Seconds $to-wait) {
MAIN($to-wait ~ 's');
}
multi sub MAIN(*@timicles where .all ~~ /<number> <[dhms]>/) {
my Seconds $to-wait = @timicles».\
split(/<number>/, :v).\
map(-> [$,Rat(Any) $count, Str(Any) $unit] --> Seconds { %unit-multipliers{$unit} * $count }).\
sum;
react {
whenever Promise.in($to-wait) {
exit 0;
}
whenever signal(SIGINT) {
exit 1;
}
whenever Supply.interval(1) {
state $count-down = $to-wait;
say ANSI-SAME-LINE ~ pretty-print($count-down--);
}
}
}
sub USAGE {
print Q:c:to/EOH/;
Usage: {$*PROGRAM-NAME} NUMBER[SUFFIX]…
Display a countdown for the specified time. Decimal fractions are supported for
NUMBER and suffixes for [d]ays, [h]ours, [m]inutes or [s]econds are
recognized. If the countdown is exhausted exit with 0.
Receiving SIGNIT will interrupt the countdown and result in exitcode 1.
EOH
}
multi sub todo($reason, $count) is export {
$todo_upto_test_num = $num_of_tests_run + $count;
$todo_reason = '# TODO ' ~ $reason;
}
multi sub todo($reason) is export {
$todo_upto_test_num = $num_of_tests_run + 1;
$todo_reason = '# TODO ' ~ $reason;
}
multi fact(0) { 1 };
multi fact($n) { $n * fact($n - 1) };
say fact(10);
multi fib(0) { 0 }
multi fib(1) { 1 }
multi fib($n) { fib($n - 1) + fib($n - 2) }
say fib(10);
multi to-json(Array $a) {
return '[ ' ~
$a.values.map({ to-json($_) }).join(', ') ~
' ]';
}
multi to-json(Hash $h) {
return '{ ' ~
$h.pairs.map({
to-json(.key) ~ ': ' ~ to-json(.value)
}).join(', ') ~
' }';
}
sub fst(*@ [$fst]){
say $fst;
}
fst(1);
fst(1,2);
sub is-in(@array, $elem) {
map({ return True if $_ == $elem }, @array);
}
my @array = 1,2,3,4,5;
is-in(@array,3);
subset Seconds of Numeric;
my regex number { \d+ [ '.' \d+ ]? }
my regex suffix { <[dhms]> }
my %unit-multipliers = 'd' => 60*60*24, 'h' => 60*60, 'm' => 60, 's' => 1;
sub MAIN(*@timicles where .all ~~ /<number> <[dhms]>/) {
my Seconds $to-wait = @timicles»\
.match(/<number> <suffix>+/)».hash\
.map(-> % ( Rat(Any) :$number, Str(Any) :$suffix ) {say $suffix; %unit-multipliers{$suffix} * $number })\
.sum;
say $to-wait ~ "s";
}
sub escape ($str) {
$_ = $str;
s:g[<-alpha-digit>] = "\\$/";
}
say escape "foobar";
{
sub escape ($str) {
$_ = $str;
s:g[<-alpha-digit>] = "\\x[{ $/.base(16) }]";
}
say escape "foo#bar?";
}
say escape "foo#bar?";
sub greet($name, $greeting = 'Ahoj') {
say "$greeting, $name!";
}
greet('Anna');
greet('Лена', 'Привет ');
sub example(@array, %hash) {
say @array.elems;
say %hash.keys.join(", ");
}
my @numbers = 1,2,3,4;
my %ages = Jnthn => 25, Noah => 120;
example(@numbers, %ages);
use fatal;
sub foo( Int @nums ) {
say @nums.join(" ");
}
sub bar( UInt @nums ) {
say @nums.join(" ");
}
my UInt @nums = (1, 2);
say "foo: ";
foo(@nums);
say "bar: ";
bar(@nums);
multi quicksort([]) { () }
multi quicksort([$pivot, *@rest]) {
my @before = @rest.grep(* < $pivot);
my @after = @rest.grep(* >= $pivot);
(quicksort(@before), $pivot, quicksort(@after))
}
my @unsorted = <13 1 9 12 4 2015>;
say quicksort(@unsorted);
sub convert_currency($amount, $rate) {
$amount = $amount * $rate;
return $amount;
}
sub convert_currency_copy($amount is copy, $rate) {
$amount = $amount * $rate;
return $amount;
}
sub convert_currency_rw($amount is rw, $rate) {
$amount = $amount * $rate;
return $amount;
}
my $price = 99;
$price = convert_currency($price, 11.1);
$price_copy = convert_currency_copy($price, 11.1);
$price_rw = convert_currency_rw($price, 11.1);
say $price;
say $price_copy;
say $price_rw;
my %hhgttg = (:40life, :41universe, :42everything);
for %hhgttg -> (:$key, :$value) {
say "$key → $value";
}
my $sig = :(Int $i, Str $s);
say (10, 'answer') ~~ $sig;
my $sub = sub ( Str $s, Int $i ) { return $s xx $i };
say $sub.signature ~~ :( Str, Int );
given $sig {
when :(Str, Int) { say 'mismatch' }
when :($, $) { say 'match' }
default { say 'no match' }
}
subset Seconds of Numeric;
my regex number { \d+ [ '.' \d+ ]? }
my regex suffix { <[dhms]> }
my %unit-multipliers = 'd' => 60*60*24, 'h' => 60*60, 'm' => 60, 's' => 1;
sub MAIN(*@timicles where .all ~~ /<number> <[dhms]>/) {
my Seconds $to-wait = @timicles»\
.match(/<number> <suffix>+/)».hash\
.map(-> % ( Rat(Any) :$number, Str(Any) :$suffix ) { %unit-multipliers{$suffix} * $number })\
.sum;
say $to-wait ~ "s";
}
subset Seconds of Numeric;
my %unit-multipliers = 'd'|'day' => 60*60*24, 'h'|'hour' => 60*60, 'm'|'min' => 60, 's'|'sec' => 1;
my @units = %unit-multipliers.keys;
my regex number { \d+ [ '.' \d+ ]? }
my regex suffix { @units }
sub MAIN(*@timicles where .all ~~ /<number> @units $/) {
my Seconds $to-wait = @timicles»\
.match(/<number> <suffix> $/)».hash
.map(-> % ( :$number, :$suffix ) { %unit-multipliers{$suffix} * $number })\
.sum;
say $to-wait ~ "s";
}
sub show_dist($from, $to, $kms as Int) {
say "From $from to $to is $kms km.";
}
show_dist('Kiev', 'Lviv', '469');
show_dist('Kiev', 'Lviv', 469.35);
sub show_dist(Str $from, Str $to, Int $kms) {
say "From $from to $to is $kms km.";
}
show_dist('Kiev', 'Lviv', 469);
show_dist(469, 'Kiev', 'Lviv');
sub head([$head, *@tail]) {
return $head;
}
sub tail([$head, *@tail]) {
return @tail;
}
my @example = 1,2,3,4;
say head(@example);
say tail(@example);
sub show_place((:$name, :$lat, :$long, *%rest)) {
say "$name lies at $lat,$long.";
say "Other facts:";
for %rest.kv -> $title, $data {
say " $title.wordcase(): $data";
}
}
my %info = name => 'Kiev', lat => 50.45,
long => 30.52, population => 2611300;
show_place(%info);
sub nd($r as Rat (:$numerator, :$denominator)) {
say "$r = $numerator/$denominator";
}
nd(4.2);
nd(3/9);
sub slurp-in-array(@ [$fst, *@rest]) {
say $fst + @rest.elems;
}
my @array = <2 3 4 5>;
slurp-in-array(@array);
sub foo(@array [$fst, $snd]) {
say "My first is $fst, my second is $snd ! All in all, I'm @array[].";
}
my @tail = 1,2;
foo(@tail);