diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-07-21 18:24:27 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-07-21 18:24:27 +0100 |
| commit | ccd96ece9ef80a31470073a8fe391442bdc96d8a (patch) | |
| tree | c538ee76ea504078f2b3a13a5170173eba04617d /challenge-017 | |
| parent | 2a0448c7b385dcd9cab4824a0e01b18a338d8049 (diff) | |
| parent | 370ae95b61f03a1fd90274f402d42c692fb2f32a (diff) | |
| download | perlweeklychallenge-club-ccd96ece9ef80a31470073a8fe391442bdc96d8a.tar.gz perlweeklychallenge-club-ccd96ece9ef80a31470073a8fe391442bdc96d8a.tar.bz2 perlweeklychallenge-club-ccd96ece9ef80a31470073a8fe391442bdc96d8a.zip | |
Merge pull request #401 from fjwhittle/master
fjwhittle Challenge 017 solutions.
Diffstat (limited to 'challenge-017')
| -rw-r--r-- | challenge-017/fjwhittle/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-017/fjwhittle/perl6/ch-1.p6 | 17 | ||||
| -rw-r--r-- | challenge-017/fjwhittle/perl6/ch-2.p6 | 89 |
3 files changed, 107 insertions, 0 deletions
diff --git a/challenge-017/fjwhittle/blog.txt b/challenge-017/fjwhittle/blog.txt new file mode 100644 index 0000000000..268592aee6 --- /dev/null +++ b/challenge-017/fjwhittle/blog.txt @@ -0,0 +1 @@ +https://rage.powered.ninja/2019/07/21/uniform-resource-parsing.html diff --git a/challenge-017/fjwhittle/perl6/ch-1.p6 b/challenge-017/fjwhittle/perl6/ch-1.p6 new file mode 100644 index 0000000000..173c98253b --- /dev/null +++ b/challenge-017/fjwhittle/perl6/ch-1.p6 @@ -0,0 +1,17 @@ +#!/usr/bin/env perl6 + +my subset Positive of Int where * > 0; + +# Ackerman function definitions +multi A(0, Positive $n) { $n + 1 } +multi A(Positive $m, 0) { A($m - 1, 1) } +multi A(Positive $m, Positive $n) { A($m - 1, A($m, $n - 1)) } + +#| Print resulting Ackerman Number with <m> and <n> +unit sub MAIN ($m = 1, $n = 2); + +# Anonymous cache handler. +&A.wrap: -> $m, $n { .[$m;$n] //= callsame } given Array.new; + +say A($m, $n); + diff --git a/challenge-017/fjwhittle/perl6/ch-2.p6 b/challenge-017/fjwhittle/perl6/ch-2.p6 new file mode 100644 index 0000000000..d36fd6a661 --- /dev/null +++ b/challenge-017/fjwhittle/perl6/ch-2.p6 @@ -0,0 +1,89 @@ +grammar G::URLish { + regex TOP { + ^ <scheme> < : :// > + <userinfo>? + [ <host> [ ':' <port> ]? '/' ]? + <path> + [ '?' <query> ]? + [ '#' <fragment> ] ? $ + } + proto regex userinfo { * } + regex userinfo:sym<user-pass> { <user> [ ':' <password> ]? '@' } + regex user { <.xpalpha>+ } + regex password { <.xpalpha>+ } + regex host { <.xpalpha>+ } + token port { <.digit>+ } + # For compatibility with the example, but I don't think : is valid here. + regex scheme { <.ialpha>+ [ ':' <.ialpha>+ ]? } + regex path { [ <.xpalpha>* ]+ % '/' } + regex query { <param>+ % <[\&;]> } + regex param { $<key> = [ <.xalpha>+ ] [ '=' $<value> = [ <.xalpha>+ ] ]? } + token fragment { <.xalpha>+ } + token xalpha { <.alpha> | <.digit> | <.safe> | <.extra> | <.escape> } + token xpalpha { <.xalpha> | '+' } + token ialpha { <.alpha> [ <.xalpha>+ ] } + token alpha { <[a..z A..Z]> } + token digit { <[0..9]> } + token safe { <[$ \- _ @ . &]> } + token extra { <[! * " ' ( ) ,]> } + token reserved { <[= ; / # ? : ]> || ' ' } + token escape { '%' <[0..9 a..z A..Z]> ** 2 } + token national { <[{ } | \[ \] \\ ^ ~]> } + token punctuation { <[< >]> } +} + +class URL::UserInfo { + has $.user is required; + has $.password; +} + +my subset Port of Int where 0 < * < 65536; + +class URL { + has Str $.scheme is required; + has URL::UserInfo $.userinfo; + has Str $.host; + has Port $.port; + has Str $.path is required; + has %.query; + has Str $.fragment; +} + +my sub urldecode($_) { + S:g/ '%' ( <[a..f A..F 0..9]> ** 2 )/{(~$0).parse-base(16).chr()}/; +} + +class A::URLish { + method userinfo:sym<user-pass>($/) { + make URL::UserInfo.new( + :user(~$<user>) + :password(~($<password> || '') || Nil) + ); + } + + method TOP($/) { + my $userinfo = $<userinfo>.?made || URL::UserInfo; + + my %query; + + if $<query><param> { + for $<query><param> { + %query.push: urldecode(~ .<key>) => .<value> ?? urldecode(~ .<value>) !! True; + } + } + + make URL.new( + :scheme(~ $<scheme> ) + :$userinfo + :host(~ ($<host> || '') || Nil ) + :port(+ ($<port> || 0 ) || Nil ) + :path(~ $<path> ) + :%query + :fragment(~ ($<fragment> || '') || Nil) + ) + } +} + +sub MAIN($path = 'jdbc:mysql://user:password@localhost:3306/pwc?profile=true#h1') { + say G::URLish.parse($path, actions => A::URLish.new).made.perl; +} |
