Update all new Tasks

This commit is contained in:
Ingy döt Net 2015-02-20 09:02:09 -05:00
parent 00a190b0a6
commit 91df62d461
5697 changed files with 93386 additions and 804 deletions

35
Task/Amb/Perl/amb-1.pl Normal file
View file

@ -0,0 +1,35 @@
use strict;
use warnings;
use constant EXIT_FAILURE => 1;
use constant EXIT_SUCCESS => 0;
sub amb {
exit(EXIT_FAILURE) if !@_;
for my $word (@_) {
my $pid = fork;
die $! unless defined $pid;
return $word if !$pid;
my $wpid = waitpid $pid, 0;
die $! unless $wpid == $pid;
exit(EXIT_SUCCESS) if $? == EXIT_SUCCESS;
}
exit(EXIT_FAILURE);
}
sub joined {
my ($join_a, $join_b) = @_;
substr($join_a, -1) eq substr($join_b, 0, 1);
}
my $w1 = amb(qw(the that a));
my $w2 = amb(qw(frog elephant thing));
my $w3 = amb(qw(walked treaded grows));
my $w4 = amb(qw(slowly quickly));
amb() unless joined $w1, $w2;
amb() unless joined $w2, $w3;
amb() unless joined $w3, $w4;
print "$w1 $w2 $w3 $w4\n";
exit(EXIT_SUCCESS);

40
Task/Amb/Perl/amb-2.pl Normal file
View file

@ -0,0 +1,40 @@
use strict;
use warnings;
sub amb {
if( @_ == 0 ) {
no warnings 'exiting';
next AMB;
}
my $code = pop;
my @words = @_;
my @index = (0) x @words;
AMB: while( 1 ) {
my @w = map $words[$_][$index[$_]], 0 .. $#_;
return $code->( @w );
} continue {
my $i = 0;
while( ++$index[$i] == @{$words[$i]} ) {
$index[$i] = 0;
return if ++$i == @index;
}
}
}
my @w1 = qw(the that a);
my @w2 = qw(frog elephant thing);
my @w3 = qw(walked treaded grows);
my @w4 = qw(slowly quickly);
sub joined {
my ($join_a, $join_b) = @_;
substr($join_a, -1) eq substr($join_b, 0, 1);
}
amb( \(@w1, @w2, @w3, @w4), sub {
my ($w1, $w2, $w3, $w4) = @_;
amb() unless joined($w1, $w2);
amb() unless joined($w2, $w3);
amb() unless joined($w3, $w4);
print "$w1 $w2 $w3 $w4\n";
});