aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author冯昶 <seaker@qq.com>2021-05-10 18:44:40 +0800
committer冯昶 <seaker@qq.com>2021-05-10 18:44:40 +0800
commit144a4fa80fbfa1d1356d05d1da1a1e7ff4cd6ba9 (patch)
treea58c8a85c4fa5a9696187dcdd0ff4a9ac36e4a82
parent3ba59e2c4ac168b4a6983cb22300568b9b73fac7 (diff)
downloadperlweeklychallenge-club-144a4fa80fbfa1d1356d05d1da1a1e7ff4cd6ba9.tar.gz
perlweeklychallenge-club-144a4fa80fbfa1d1356d05d1da1a1e7ff4cd6ba9.tar.bz2
perlweeklychallenge-club-144a4fa80fbfa1d1356d05d1da1a1e7ff4cd6ba9.zip
add comments
-rwxr-xr-xchallenge-112/feng-chang/raku/ch-1.raku10
-rwxr-xr-xchallenge-112/feng-chang/raku/ch-2.raku4
2 files changed, 7 insertions, 7 deletions
diff --git a/challenge-112/feng-chang/raku/ch-1.raku b/challenge-112/feng-chang/raku/ch-1.raku
index 5175350730..cf4cf08e00 100755
--- a/challenge-112/feng-chang/raku/ch-1.raku
+++ b/challenge-112/feng-chang/raku/ch-1.raku
@@ -1,20 +1,20 @@
#!/bin/env raku
sub MAIN(Str:D $path where * ~~ rx{^'/'}) {
- my @dirs = $path.split(rx{'/'+}).tail(*-1);
- @dirs.pop unless @dirs[*-1];
+ my @dirs = $path.split(rx{'/'+}).tail(*-1); # put all directory names into an array, omit first empty element
+ @dirs.pop unless @dirs[*-1]; # omit last element if it is empty
my $i = 0;
try {
while $i < @dirs.elems {
given @dirs[$i] {
- when '.' { @dirs.splice($i, 1) }
- when '..' { @dirs.splice($i-1, 2); --$i; }
+ when '.' { @dirs.splice($i, 1) } # omit '.'
+ when '..' { @dirs.splice($i-1, 2); --$i; } # omit '..' and its upper directory
default { ++$i }
}
}
CATCH {
- $*ERR.put: 'cannot walk beyond root';
+ $*ERR.put: 'cannot walk beyond root'; # guard against excessive '..'s
exit;
}
}
diff --git a/challenge-112/feng-chang/raku/ch-2.raku b/challenge-112/feng-chang/raku/ch-2.raku
index f0ea1a093b..b674d7b518 100755
--- a/challenge-112/feng-chang/raku/ch-2.raku
+++ b/challenge-112/feng-chang/raku/ch-2.raku
@@ -1,13 +1,13 @@
#!/bin/env raku
-proto walk(UInt:D --> UInt:D) {*}
+proto walk(UInt:D --> UInt:D) {*} # restrict dispatch pattern
multi walk(0) { 1 }
multi walk(1) { 1 }
multi walk(2) { 2 }
multi walk($n) {
- state @memo;
+ state @memo; # use memory to speed up counting
@memo[$n] = walk($n-1) + walk($n-2) unless @memo[$n].defined;
@memo[$n];
}