diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-04-29 13:44:27 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-04-29 13:44:27 +0100 |
| commit | 7aeae3af6a534a52d43a699b22ed73c2552ff1ff (patch) | |
| tree | b53b03b8d59cc34dfebed57deb12b94f983f15c1 | |
| parent | 647042ed7d72f7f346e73fc444543f67b286a7f1 (diff) | |
| parent | 844e8bf1c77f3a499b49d3be3ccab96f535ebaf3 (diff) | |
| download | perlweeklychallenge-club-7aeae3af6a534a52d43a699b22ed73c2552ff1ff.tar.gz perlweeklychallenge-club-7aeae3af6a534a52d43a699b22ed73c2552ff1ff.tar.bz2 perlweeklychallenge-club-7aeae3af6a534a52d43a699b22ed73c2552ff1ff.zip | |
Merge pull request #1651 from Scimon/master
Been a busy week. Finally got this one done.
| -rw-r--r-- | challenge-058/simon-proctor/raku/ch-1.raku | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/challenge-058/simon-proctor/raku/ch-1.raku b/challenge-058/simon-proctor/raku/ch-1.raku new file mode 100644 index 0000000000..26c150ecf7 --- /dev/null +++ b/challenge-058/simon-proctor/raku/ch-1.raku @@ -0,0 +1,49 @@ +#!/usr/bin/env raku + +use v6; + +my $matcher = / ^ $<ver> = (\d+)+ % "." ( "_" $<patch> = (\d+) ) ? $ /; + +subset ValidVer of Str where * ~~ $matcher; + +class PWCVersion { + + has @.points; + has Int $.patch; + multi method new ( ::?CLASS:U: ValidVer $str ) { + my $match = $str ~~ $matcher; + + if defined $match[0]<patch> { + self.bless( + :points( $match<ver>.values.map(*.Int) ), + :patch( $match[0]<patch>.Int ) + ); + } else { + self.bless( + :points( $match<ver>.values.map(*.Int) ), + ); + } + } + + method Str () { + "{@!points.join('.')}{(defined $!patch)?? '_' ~ $!patch !! '' }" + } + + method gist() { self.Str } +} + +multi sub infix:<cmp> ( PWCVersion $v1, PWCVersion $v2 ) { + return $v1.points cmp $v2.points unless ($v1.points cmp $v2.points) ~~ Same; + + return Same if ! $v1.patch && ! $v2.patch; + return More if $v1.patch && ! $v2.patch; + return Less if ! $v1.patch && $v2.patch; + + return $v1.patch <=> $v2.patch; +} + +sub MAIN( ValidVer $str1, ValidVer $str2 ) { + my $v1 = PWCVersion.new($str1); + my $v2 = PWCVersion.new($str2); + say "$v1 <=> $v2 : {+($v1 cmp $v2)}"; +} |
