aboutsummaryrefslogtreecommitdiff
path: root/challenge-044
diff options
context:
space:
mode:
authorSimon Proctor <simon.proctor@zpg.co.uk>2020-01-20 10:44:58 +0000
committerSimon Proctor <simon.proctor@zpg.co.uk>2020-01-20 10:44:58 +0000
commit6d5027a7cbf1bc0c53b0522152e662fff5def033 (patch)
tree23ed2b05aa05568071772fe9bcc829010b9b03cd /challenge-044
parent008c778923279b75b7eebf43cd5fb392bcba583c (diff)
downloadperlweeklychallenge-club-6d5027a7cbf1bc0c53b0522152e662fff5def033.tar.gz
perlweeklychallenge-club-6d5027a7cbf1bc0c53b0522152e662fff5def033.tar.bz2
perlweeklychallenge-club-6d5027a7cbf1bc0c53b0522152e662fff5def033.zip
Challenge number 1. Functional Raku for the win
Diffstat (limited to 'challenge-044')
-rw-r--r--challenge-044/simon-proctor/raku/ch-1.p658
1 files changed, 58 insertions, 0 deletions
diff --git a/challenge-044/simon-proctor/raku/ch-1.p6 b/challenge-044/simon-proctor/raku/ch-1.p6
new file mode 100644
index 0000000000..6766323b24
--- /dev/null
+++ b/challenge-044/simon-proctor/raku/ch-1.p6
@@ -0,0 +1,58 @@
+#!/usr/bin/env perl6
+
+use v6.d;
+
+multi sub concat( \a, \b ) { a ~ b }
+multi sub concat( \a, @b ) { ( a ~ @b[0], @b[1..*] ) }
+
+my %ops-map = (
+ '+' => &infix:<+>,
+ '-' => &infix:<->,
+ ',' => &infix:<,>,
+ '~' => &concat,
+);
+
+multi sub selections(0, *@) is pure {
+ return [];
+}
+
+multi sub selections(1,*@ops) is pure {
+ return [[@ops[0]],[@ops[1]]];
+}
+
+multi sub selections(UInt \c, *@ops) is pure {
+ lazy gather {
+ my @recur = selections(c-1, @ops);
+ for selections(1, @ops) -> @prefix {
+ for @recur -> @list {
+ take [|@prefix, |@list];
+ }
+ }
+ }
+}
+
+multi sub apply-ops( @list where *.elems == 2, @ops ) {
+ return @ops[0]( @list[0], @list[1] );
+}
+
+multi sub apply-ops( @list, @ops ) {
+ return @ops[0](@list[0],apply-ops( @list[1..*], @ops[1..*] ) );
+}
+
+sub display( @nums, @ops ) {
+ my $result = @nums.map( -> $v { $v ~ (@ops ?? @ops.shift !! '') } ).join("");
+}
+
+my @list = "1".."9";
+
+my @inputs = selections(@list.elems-1, ',', '~').map( -> @sel { apply-ops( @list, @sel.map( { %ops-map{$_} } ) ).flat } );
+
+for @inputs -> @test {
+ for selections(@test.elems-1, '+', '-') -> @ops {
+ my $result = apply-ops( @test, @ops.map( { %ops-map{$_} } ) );
+ if ( $result ~~ 100 ) {
+ say "{display(@test,@ops)} = $result";
+ exit;
+ }
+ }
+}