aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-06-15 06:10:07 +0100
committerGitHub <noreply@github.com>2019-06-15 06:10:07 +0100
commit84fa62f1520f26aef0960d19761d7196a7d55700 (patch)
treedf0d3e591938728fdb92b273f34ac80278d2adca
parent19ee55a78911c10c4f2821f45022a8fb983ea65d (diff)
parentc97091a793a56c9def7c71beb66f5d7541f740f6 (diff)
downloadperlweeklychallenge-club-84fa62f1520f26aef0960d19761d7196a7d55700.tar.gz
perlweeklychallenge-club-84fa62f1520f26aef0960d19761d7196a7d55700.tar.bz2
perlweeklychallenge-club-84fa62f1520f26aef0960d19761d7196a7d55700.zip
Merge pull request #251 from fjwhittle/master
fjwhittle challenge 012 solutions
-rw-r--r--challenge-012/fjwhittle/perl6/ch-1.p69
-rw-r--r--challenge-012/fjwhittle/perl6/ch-2.p613
-rw-r--r--challenge-012/fjwhittle/perl6/ch-3.p637
3 files changed, 59 insertions, 0 deletions
diff --git a/challenge-012/fjwhittle/perl6/ch-1.p6 b/challenge-012/fjwhittle/perl6/ch-1.p6
new file mode 100644
index 0000000000..933c9829bc
--- /dev/null
+++ b/challenge-012/fjwhittle/perl6/ch-1.p6
@@ -0,0 +1,9 @@
+# MOAR LAZY LISTS!
+
+# Primed list (double entendre πŸ˜‰)
+my \P := (^Inf).grep: *.is-prime;
+
+# Generate Euclid numbers,
+(^Inf).map(-> $i { ([*] P[0..$i]) + 1})\
+ .grep(!*.is-prime)[0]\ # filtering by the first non-prime,
+ .put; # and output.
diff --git a/challenge-012/fjwhittle/perl6/ch-2.p6 b/challenge-012/fjwhittle/perl6/ch-2.p6
new file mode 100644
index 0000000000..4d6cafa8b7
--- /dev/null
+++ b/challenge-012/fjwhittle/perl6/ch-2.p6
@@ -0,0 +1,13 @@
+# Operate on paths provided from $*IN, split into components.
+# Output is then joined by the system directory separator.
+
+$*IN.lines.map({ [.chomp.split($*SPEC.dir-sep)] }).reduce({
+ # Find the minimum number of components
+ my $n = min($^a.elems, $^b.elems);
+
+ # Reduce components while the array heads don't match
+ $n-- while $n > 0 and $^a[^$n] !~~ $^b[^$n];
+
+ # Reduced result is the maximum matching array head;
+ $^a[^$n];
+}).join($*SPEC.dir-sep).put;
diff --git a/challenge-012/fjwhittle/perl6/ch-3.p6 b/challenge-012/fjwhittle/perl6/ch-3.p6
new file mode 100644
index 0000000000..d0b605078f
--- /dev/null
+++ b/challenge-012/fjwhittle/perl6/ch-3.p6
@@ -0,0 +1,37 @@
+use Cro::HTTP::Client;
+
+# I started this on the weekend, so don't have an API key for testing.
+# Instead I've verified my assumptions using jsonplaceholder.typicode.com
+
+#| Find synonyms for a given word using the Synonyms API from STANDS4
+unit sub MAIN(
+ Str $word, #= find synonyms for this word
+ Str :$uid!, #= STANDS4 user ID
+ Str :$token!, #= STANDS4 API token
+);
+
+# I think Cro should have one of these already, but couldn't find it documented
+sub url-part-encode (Str $in --> Str) {
+ $in.trans: <! # $ % & ' ( ) * + , / : ; = ? @ [ ]> => <%21 %23 %24 %25 %26 %27 %28 %29 %2A %2B %2C %2F %3A %3B %3D %3F %40 %5B %5D>
+}
+
+# Fill in request URI with url encoded arguments
+my $uri = 'http://www.stands4.com/services/v2/syno.php?uid=%s&tokenid=%s&word=%s&format=json'.sprintf: ($uid, $token, $word).map(&url-part-encode);
+
+# Service request is F.A.B.
+my $result = await(Cro::HTTP::Client.get($uri).then({await .result.body}));
+
+# Our service doesn't know how to use HTTP response codes.
+# This bit is tested.
+$result<error> and die "API responded with error β€˜$result<error>’";
+
+my $synonyms = $result<results><result><synonyms>;
+
+say "Synonyms for β€˜$word’ using the Synonyms API from STANDS4:";
+say $synonyms;
+
+CATCH {
+ when X::Cro::HTTP::Error {
+ die "Error in request: β€˜$_’";
+ }
+}