3 use aliased 'DX::Op::FromCode';
4 use aliased 'DX::ArrayStream';
18 my @shells = qw(csh bash);
21 bash => { map +($_ => 1),
22 qw(joe.example.com kitty.scsys.co.uk) },
23 csh => { map +($_ => 1),
24 qw(jim.example.com joe.example.com bob.example.com) },
28 my ($var, $array) = @_;
30 my ($self, $state) = @_;
31 $state->bind_stream_then(
32 $state->scope_var($var),
33 ArrayStream->from_array(@$array),
40 my ($vars, $test) = @_;
42 my ($self, $state) = @_;
43 my @values = map $state->scope_var($_)->bound_value, @$vars;
44 if ($test->(@values)) {
45 return $state->then($self->next);
47 return $state->backtrack;
54 code => bind_array(S => \@servers),
55 next => FromCode->new(
56 code => test_values([ 'S' ], sub { $_[0] =~ /\.example\.com$/ }),
67 my %scope = map +($_ => $_), @{$vars};
68 my %by_id = map +($_ => DX::Var->new(id => $_)), @{$vars};
79 my $stream = DX::ResultStream->new(for_state => make_state([ 'S' ], $op));
81 is($stream->next->{'S'}, $_)
82 for qw(jim.example.com joe.example.com bob.example.com);
84 is($stream->next, undef, 'No more');
86 my $complex_op = FromCode->new(
87 code => bind_array(S => \@servers),
88 next => FromCode->new(
89 code => test_values([ 'S' ], sub { $_[0] =~ /\.example\.com$/ }),
90 next => FromCode->new(
91 code => bind_array(P => \@shells),
92 next => FromCode->new(
93 code => test_values([ qw(S P) ], sub { $shells{$_[1]}{$_[0]} }),
99 my $cstream = DX::ResultStream->new(
100 for_state => make_state([ qw(S P) ], $complex_op)
104 [ $cstream->results ],
106 { P => 'csh', S => 'jim.example.com' },
107 { P => 'csh', S => 'joe.example.com' },
108 { P => 'bash', S => 'joe.example.com' },
109 { P => 'csh', S => 'bob.example.com' },
114 my $pop_stack = FromCode->new(
115 code => sub { $_[1]->pop_return_stack }
118 my $inner_op = make_op($pop_stack);
120 my $call_op = FromCode->new(
122 my ($self, $state) = @_;
123 my $save_scope = $state->scope;
124 my %scope = (S => $save_scope->{S});
125 my $ret_op = FromCode->new(
126 code => sub { $_[1]->but(scope => $save_scope, next_op => $_[0]->next) },
129 $state->but(scope => \%scope)->push_return_then($ret_op, $inner_op);
131 next => FromCode->new(
132 code => bind_array(P => \@shells),
133 next => FromCode->new(
134 code => test_values([ qw(S P) ], sub { $shells{$_[1]}{$_[0]} }),
139 my $callstream = DX::ResultStream->new(
140 for_state => make_state([ qw(S P) ], $call_op)
144 [ $callstream->results ],
146 { P => 'csh', S => 'jim.example.com' },
147 { P => 'csh', S => 'joe.example.com' },
148 { P => 'bash', S => 'joe.example.com' },
149 { P => 'csh', S => 'bob.example.com' },
154 my $has_csh = FromCode->new(
155 code => test_values([ 'S' ], sub { $shells{csh}{$_[0]} }),
158 my $has_bash = FromCode->new(
159 code => test_values([ 'S' ], sub { $shells{bash}{$_[0]} }),
164 my ($self, $state) = @_;
165 my $var = DX::Var->new(id => 'OR')->with_stream(
166 my $stream = ArrayStream->from_array($has_csh, $has_bash)
168 my $inner_or = FromCode->new(
169 code => sub { $_[1]->then($var->bound_value) }
171 $state->push_return_then($self->next, $inner_or)
175 my $top_or = FromCode->new(
176 code => bind_array(S => \@servers),
177 next => FromCode->new(code => $or_code),
180 my $orstream = DX::ResultStream->new(
181 for_state => make_state([ qw(S) ], $top_or)
185 [ $orstream->results ],
188 S => "kitty.scsys.co.uk"
191 S => "jim.example.com"
194 S => "joe.example.com"
197 S => "joe.example.com"
200 S => "bob.example.com"
206 my $top_or_2 = FromCode->new(
207 code => bind_array(S => \@servers),
208 next => FromCode->new(
210 next => FromCode->new(
211 code => test_values([ 'S' ], sub { $_[0] =~ /\.example\.com$/ }),
216 my $orstream_2 = DX::ResultStream->new(
217 for_state => make_state([ qw(S) ], $top_or_2)
221 [ $orstream_2->results ],
224 S => "jim.example.com"
227 S => "joe.example.com"
230 S => "joe.example.com"
233 S => "bob.example.com"