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->then($self->next)->bind_stream(
32 $state->scope_var($var),
33 ArrayStream->from_array(@$array),
39 my ($vars, $test) = @_;
41 my ($self, $state) = @_;
42 my @values = map $state->scope_var($_)->bound_value, @$vars;
43 if ($test->(@values)) {
44 return $state->then($self->next);
46 return $state->backtrack;
53 code => bind_array(S => \@servers),
54 next => FromCode->new(
55 code => test_values([ 'S' ], sub { $_[0] =~ /\.example\.com$/ }),
66 my %scope = map +($_ => $_), @{$vars};
67 my %by_id = map +($_ => DX::Var->new(id => $_)), @{$vars};
78 my $stream = DX::ResultStream->new(for_state => make_state([ 'S' ], $op));
80 is($stream->next->value_for('S'), $_)
81 for qw(jim.example.com joe.example.com bob.example.com);
83 is($stream->next, undef, 'No more');
85 my $complex_op = FromCode->new(
86 code => bind_array(S => \@servers),
87 next => FromCode->new(
88 code => test_values([ 'S' ], sub { $_[0] =~ /\.example\.com$/ }),
89 next => FromCode->new(
90 code => bind_array(P => \@shells),
91 next => FromCode->new(
92 code => test_values([ qw(S P) ], sub { $shells{$_[1]}{$_[0]} }),
102 map +($_ => $v->value_for($_)), $v->var_names,
107 my $cstream = DX::ResultStream->new(
108 for_state => make_state([ qw(S P) ], $complex_op)
112 [ bound_values $cstream->results ],
114 { P => 'csh', S => 'jim.example.com' },
115 { P => 'csh', S => 'joe.example.com' },
116 { P => 'bash', S => 'joe.example.com' },
117 { P => 'csh', S => 'bob.example.com' },
122 my $pop_stack = FromCode->new(
123 code => sub { $_[1]->pop_return_stack }
126 my $inner_op = make_op($pop_stack);
128 my $call_op = FromCode->new(
130 my ($self, $state) = @_;
131 my $save_scope = $state->scope;
132 my %scope = (S => $save_scope->{S});
133 my $ret_op = FromCode->new(
134 code => sub { $_[1]->but(scope => $save_scope, next_op => $_[0]->next) },
137 $state->but(scope => \%scope)->push_return_then($ret_op, $inner_op);
139 next => FromCode->new(
140 code => bind_array(P => \@shells),
141 next => FromCode->new(
142 code => test_values([ qw(S P) ], sub { $shells{$_[1]}{$_[0]} }),
147 my $callstream = DX::ResultStream->new(
148 for_state => make_state([ qw(S P) ], $call_op)
152 [ bound_values $callstream->results ],
154 { P => 'csh', S => 'jim.example.com' },
155 { P => 'csh', S => 'joe.example.com' },
156 { P => 'bash', S => 'joe.example.com' },
157 { P => 'csh', S => 'bob.example.com' },
162 my $has_csh = FromCode->new(
163 code => test_values([ 'S' ], sub { $shells{csh}{$_[0]} }),
166 my $has_bash = FromCode->new(
167 code => test_values([ 'S' ], sub { $shells{bash}{$_[0]} }),
172 my ($self, $state) = @_;
173 my $var = DX::Var->new(id => 'OR')->with_stream(
174 my $stream = ArrayStream->from_array($has_csh, $has_bash)
176 my $inner_or = FromCode->new(
177 code => sub { $_[1]->then($var->bound_value) }
179 $state->push_return_then($self->next, $inner_or)
183 my $top_or = FromCode->new(
184 code => bind_array(S => \@servers),
185 next => FromCode->new(code => $or_code),
188 my $orstream = DX::ResultStream->new(
189 for_state => make_state([ qw(S) ], $top_or)
193 [ bound_values $orstream->results ],
196 S => "kitty.scsys.co.uk"
199 S => "jim.example.com"
202 S => "joe.example.com"
205 S => "joe.example.com"
208 S => "bob.example.com"
214 my $top_or_2 = FromCode->new(
215 code => bind_array(S => \@servers),
216 next => FromCode->new(
218 next => FromCode->new(
219 code => test_values([ 'S' ], sub { $_[0] =~ /\.example\.com$/ }),
224 my $orstream_2 = DX::ResultStream->new(
225 for_state => make_state([ qw(S) ], $top_or_2)
229 [ bound_values $orstream_2->results ],
232 S => "jim.example.com"
235 S => "joe.example.com"
238 S => "joe.example.com"
241 S => "bob.example.com"