--- /dev/null
+package DX::ResultStream;
+
+use Moo;
+
+has for_state => (is => 'ro', required => 1);
+
+has _current_state => (is => 'rw');
+
+has is_exhausted => (is => 'rwp');
+
+sub next {
+ my ($self) = @_;
+ return if $self->is_exhausted;
+ my $start_state = do {
+ if (my $cur = $self->_current_state) {
+ $cur->backtrack;
+ } else {
+ $self->for_state
+ }
+ };
+ my $state = $self->_current_state($start_state->run);
+ return +{
+ map +($_ => $state->scope_var($_)->bound_value), keys %{$state->scope}
+ };
+}
+
+1;
sub backtrack {
my ($self) = @_;
- while (my ($state, $var) = @{$self->last_choice}) {
+ my ($state, $var) = ($self);
+ while (($state, $var) = @{$state->last_choice}) {
$var->bound_value; $var->clear_bound_value;
- return $state unless $var->bound_stream->is_exhausted;
+ return $state->mark_choice($var) unless $var->bound_stream->is_exhausted;
}
die "Out of options";
}
$self->new(%$self, next_op => $then);
}
+sub run {
+ my ($state) = @_;
+ while (my $op = $state->next_op) {
+ $state = $op->run($state);
+ }
+ return $state;
+}
+
1;
use Test::More;
use aliased 'DX::Op::FromCode';
use aliased 'DX::ArrayStream';
+use DX::ResultStream;
use DX::Var;
use DX::State;
+use Test::Exception;
my @servers = qw(
kitty.scsys.co.uk
last_choice => []
);
-while (my $op = $state->next_op) {
- $state = $op->run($state);
- ::Dwarn($state);
-}
+my $stream = DX::ResultStream->new(for_state => $state);
+
+is($stream->next->{'S'}, $_)
+ for qw(jim.example.com joe.example.com bob.example.com);
-is($state->scope_var('S')->bound_value, 'jim.example.com');
+dies_ok { $stream->next } 'No more';
done_testing;