in reply to How implement AMB in perl?
use warnings; use strict; use Carp; sub need { my $cond = shift; croak 'require failed' if (!$cond); } sub which_1 { my ($code, $vals) = @_; for my $param (@{$vals}) { if (my $res = eval { &{$code}($param); return 1; }) { return $param; } } croak 'no match'; } my @floors = (1..5); print which_1 sub { my $p = shift; need($p == 3); }, \@floors;
The which_1 sub returns whichever element of an array (passed as a reference in a second arg) first matches all the needs in the code reference (first arg).
One can generalize this to an arbitrary number of array references using currying. I may do this later, when I have more time.
Remember — continuations can (and should) usually be replaced with closures or threads.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How implement AMB in perl?
by blazar (Canon) on Apr 29, 2007 at 12:42 UTC |