From 465b20ee77fef30d21b4af76553996bc3de2b720 Mon Sep 17 00:00:00 2001 From: Daniel Mita Date: Tue, 19 Nov 2019 13:59:14 +0000 Subject: Add Raku solutions for challenge-035 --- challenge-035/daniel-mita/perl6/Morse.pm6 | 92 +++++++++++++++++++++++++++++++ challenge-035/daniel-mita/perl6/ch-1.p6 | 19 +++++++ challenge-035/daniel-mita/perl6/ch-2.p6 | 19 +++++++ 3 files changed, 130 insertions(+) create mode 100644 challenge-035/daniel-mita/perl6/Morse.pm6 create mode 100755 challenge-035/daniel-mita/perl6/ch-1.p6 create mode 100755 challenge-035/daniel-mita/perl6/ch-2.p6 diff --git a/challenge-035/daniel-mita/perl6/Morse.pm6 b/challenge-035/daniel-mita/perl6/Morse.pm6 new file mode 100644 index 0000000000..375fd03955 --- /dev/null +++ b/challenge-035/daniel-mita/perl6/Morse.pm6 @@ -0,0 +1,92 @@ +my role X::Morse is Exception { + has $.payload; + method message { 'Morse error' } +} + +my class X::Morse::InvalidCharacter does X::Morse { + method message { "Could not parse character: $!payload" } +} + +module Morse { + my constant %values = ( + A => 0b10111, + B => 0b111010101, + C => 0b11101011101, + D => 0b1110101, + E => 0b1, + F => 0b101011101, + G => 0b111011101, + H => 0b1010101, + I => 0b101, + J => 0b1011101110111, + K => 0b111010111, + L => 0b101110101, + M => 0b1110111, + N => 0b11101, + O => 0b11101110111, + P => 0b10111011101, + Q => 0b1110111010111, + R => 0b1011101, + S => 0b10101, + T => 0b111, + U => 0b1010111, + V => 0b101010111, + W => 0b101110111, + X => 0b11101010111, + Y => 0b1110101110111, + Z => 0b11101110101, + 1 => 0b10111011101110111, + 2 => 0b101011101110111, + 3 => 0b1010101110111, + 4 => 0b10101010111, + 5 => 0b101010101, + 6 => 0b11101010101, + 7 => 0b1110111010101, + 8 => 0b111011101110101, + 9 => 0b11101110111011101, + 0 => 0b1110111011101110111, + # TODO: punctuation + ); + my constant %chars = %values.antipairs; + + grammar Decode { + token TOP { + % '0' ** 7 } + token word { + % '000' } + token char { + % '0' } + proto token mark {*} + token mark:sym { '1' } + token mark:sym { '111' } + } + + class Decoder { + method TOP ($/) { + make $.map(*.made).join(' '); + } + + method word ($/) { + make $.map(*.made).join; + } + + method char ($/) { + make %chars{ $/.Str.parse-base(2) } + or X::Morse::InvalidCharacter.new( :payload($/) ).throw; + } + } + + #| Decode binary morse code + sub decode ( + Str $bits where *.comb.all eq 1|0, #= A string containing a sequence of 1s and 0s + --> Str + ) is export { + return Decode.parse( $bits, :actions(Decoder.new) ).made or + X::Morse.new.throw; + } + + #| Encode binary morse code + sub encode ( + Str $phrase, + --> Str + ) is export { + X::NYI.new( :feature(&?ROUTINE.name) ).throw; + } +} diff --git a/challenge-035/daniel-mita/perl6/ch-1.p6 b/challenge-035/daniel-mita/perl6/ch-1.p6 new file mode 100755 index 0000000000..9b2a502a05 --- /dev/null +++ b/challenge-035/daniel-mita/perl6/ch-1.p6 @@ -0,0 +1,19 @@ +#!/usr/bin/env perl6 +use v6.d; +use lib $?FILE.IO.dirname; +use Morse; + +# My intention to take &encode directly from Morse and +# use it as MAIN here. This does what I intended, but +# I'm unsure if it was the best way to go about it. + +sub MAIN ( + |c where * ~~ &encode.signature, + --> Nil +) { + encode(||c).say; +} + +sub GENERATE-USAGE(|) { + RUN-MAIN(&encode, Nil); +} diff --git a/challenge-035/daniel-mita/perl6/ch-2.p6 b/challenge-035/daniel-mita/perl6/ch-2.p6 new file mode 100755 index 0000000000..7a72e65f2b --- /dev/null +++ b/challenge-035/daniel-mita/perl6/ch-2.p6 @@ -0,0 +1,19 @@ +#!/usr/bin/env perl6 +use v6.d; +use lib $?FILE.IO.dirname; +use Morse; + +# My intention to take &decode directly from Morse and +# use it as MAIN here. This does what I intended, but +# I'm unsure if it was the best way to go about it. + +sub MAIN ( + |c where * ~~ &decode.signature, + --> Nil +) { + decode(||c).say; +} + +sub GENERATE-USAGE(|) { + RUN-MAIN(&decode, Nil); +} -- cgit