aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Proctor <simon.proctor@zoopla.co.uk>2020-05-26 10:05:47 +0100
committerSimon Proctor <simon.proctor@zoopla.co.uk>2020-05-26 10:05:47 +0100
commitd9e45520f9ac841202b4be464680f667f0ff910b (patch)
treeed564f305cb03bc0fff73b8e66d968b9a4fd5bb1
parent7a4f1b300021f466546ae79ffd14e68e63865053 (diff)
downloadperlweeklychallenge-club-d9e45520f9ac841202b4be464680f667f0ff910b.tar.gz
perlweeklychallenge-club-d9e45520f9ac841202b4be464680f667f0ff910b.tar.bz2
perlweeklychallenge-club-d9e45520f9ac841202b4be464680f667f0ff910b.zip
Challenge 1. Emails
-rw-r--r--challenge-062/simon-proctor/raku/ch-1.example.txt5
-rw-r--r--challenge-062/simon-proctor/raku/ch-1.raku50
2 files changed, 55 insertions, 0 deletions
diff --git a/challenge-062/simon-proctor/raku/ch-1.example.txt b/challenge-062/simon-proctor/raku/ch-1.example.txt
new file mode 100644
index 0000000000..5dc3f2ceb0
--- /dev/null
+++ b/challenge-062/simon-proctor/raku/ch-1.example.txt
@@ -0,0 +1,5 @@
+name@example.org
+rjt@cpan.org
+Name@example.org
+rjt@CPAN.org
+user@alpha.example.org
diff --git a/challenge-062/simon-proctor/raku/ch-1.raku b/challenge-062/simon-proctor/raku/ch-1.raku
new file mode 100644
index 0000000000..0e2afe994f
--- /dev/null
+++ b/challenge-062/simon-proctor/raku/ch-1.raku
@@ -0,0 +1,50 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+role EmailAddress {
+ has Str $.value;
+ has Str $.canonical-name;
+ has Str $.canonical-domain;
+ has Str $.canonical;
+
+ submethod TWEAK() {
+ my ( $name, $domain ) = $!value.split('@');
+
+ $!canonical-name = $name;
+ $!canonical-domain = $domain.lc;
+ $!canonical = "{$!canonical-name}\@{$!canonical-domain}";
+ }
+
+ method Str () { $!value }
+ method gist () { $!value }
+}
+
+#| Take a list of files read them and return the sorted list of
+multi sub MAIN(
+ Bool :u(:$unique) = False, #= Return only unqiue addresses
+ *@files )
+{
+ process-handle( handle => IO::CatHandle.new( @files ), :$unique );
+}
+
+#| Read email address from standard input and print the sorted list when done
+multi sub MAIN(
+ Bool :u(:$unique) = False #= Return only unqiue addresses
+) {
+ process-handle( handle => $*IN, :$unique );
+}
+
+sub process-handle( :$handle, :$unique = False ) {
+ my @emails;
+ for $handle.lines -> $email {
+ @emails.push( EmailAddress.new( value => $email.chomp ) );
+ }
+ .say for sort-emails( @emails, :$unique );
+}
+
+sub sort-emails( @emails, Bool :$unique = False ) {
+ @emails.=sort( { $^a.canonical-domain cmp $^b.canonical-domain || $^a.canonical-name cmp $^b.canonical-name } );
+ @emails.=unique( as => { .canonical }, with => &[eq] ) if $unique;
+ return @emails;
+}