diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-03-03 13:58:28 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-03 13:58:28 +0000 |
| commit | 789bbb50679c7bfaf5a62ba579ba1c7718fb91c8 (patch) | |
| tree | 62402fd5bd3cdcb90a16b9bdb188de00131877de | |
| parent | e437f6ae1b4ad697009b94f8cfcdb9d7f80c7b04 (diff) | |
| parent | 189885993ced1d77ce135f01da93b0b18a1e4819 (diff) | |
| download | perlweeklychallenge-club-789bbb50679c7bfaf5a62ba579ba1c7718fb91c8.tar.gz perlweeklychallenge-club-789bbb50679c7bfaf5a62ba579ba1c7718fb91c8.tar.bz2 perlweeklychallenge-club-789bbb50679c7bfaf5a62ba579ba1c7718fb91c8.zip | |
Merge pull request #3653 from Scimon/master
Adding caching to challenge 1
| -rw-r--r-- | challenge-102/simon-proctor/raku/ch-1.raku | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/challenge-102/simon-proctor/raku/ch-1.raku b/challenge-102/simon-proctor/raku/ch-1.raku index 7e7f64f446..1056977ef7 100644 --- a/challenge-102/simon-proctor/raku/ch-1.raku +++ b/challenge-102/simon-proctor/raku/ch-1.raku @@ -22,7 +22,14 @@ multi sub MAIN( multi sub square(Int $ where * <= 0) returns Bool { False } -multi sub square(Int \i) returns Bool { my \s = i.sqrt; s == s.Int; } +my %square-cache; +my $lock = Lock.new; +multi sub square(Int \i) returns Bool { + return %square-cache{i} if defined %square-cache{i}; + my \r = i.sqrt.narrow ~~ Int; + $lock.protect( { %square-cache{i} = r } ); + return r; +} sub rare(Int \r) returns Bool { my \f = r.flip; |
