From cefe026fd521b0e64c93fc178057aca0856b2270 Mon Sep 17 00:00:00 2001 From: Francis Whittle Date: Sun, 21 Jul 2019 23:10:54 +1000 Subject: fjwhittle Challenge 017 solutions. --- challenge-017/fjwhittle/perl6/ch-1.p6 | 17 +++++++ challenge-017/fjwhittle/perl6/ch-2.p6 | 89 +++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 challenge-017/fjwhittle/perl6/ch-1.p6 create mode 100644 challenge-017/fjwhittle/perl6/ch-2.p6 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 and +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 { + ^ < : :// > + ? + [ [ ':' ]? '/' ]? + + [ '?' ]? + [ '#' ] ? $ + } + proto regex userinfo { * } + regex userinfo:sym { [ ':' ]? '@' } + 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 { + % <[\&;]> } + regex param { $ = [ <.xalpha>+ ] [ '=' $ = [ <.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($/) { + make URL::UserInfo.new( + :user(~$) + :password(~($ || '') || Nil) + ); + } + + method TOP($/) { + my $userinfo = $.?made || URL::UserInfo; + + my %query; + + if $ { + for $ { + %query.push: urldecode(~ .) => . ?? urldecode(~ .) !! True; + } + } + + make URL.new( + :scheme(~ $ ) + :$userinfo + :host(~ ($ || '') || Nil ) + :port(+ ($ || 0 ) || Nil ) + :path(~ $ ) + :%query + :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; +} -- cgit From 370ae95b61f03a1fd90274f402d42c692fb2f32a Mon Sep 17 00:00:00 2001 From: Francis Whittle Date: Mon, 22 Jul 2019 00:24:34 +1000 Subject: FJWhittle Challenge 017 blog post. --- challenge-017/fjwhittle/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-017/fjwhittle/blog.txt 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 -- cgit