HH\Asio\vfkw
Returns an Awaitable
of Vector
of ResultOrExceptionWrapper
after a
filtering operation has been applied to each key/value pair in the provided
KeyedTraversable
namespace HH\Asio;
function vfkw<Tk, T>(
KeyedTraversable<Tk, T> $inputs,
(function(Tk, T): Awaitable<bool>) $callable,
): Awaitable<Vector<ResultOrExceptionWrapper<T>>>;
This function is similar to vfk()
, except the Vector
in the returned
Awaitable
contains ResultOrExceptionWrapper
s instead of raw values.
This function is similar to Vector::filterWithKey()
, but the mapping of the
key/value pairs are done using Awaitable
s.
This function is called vfkw
because we are returning a v
ector, doing a
f
iltering operation that includes both k
eys and values, and each member
of the Vector
is w
rapped by a ResultOrExceptionWrapper
.
$callable
must return an Awaitable
of bool
.
The ResultOrExceptionWrapper
s in the Vector
of the returned Awaitable
are not available until you await
or join
the returned Awaitable
.
Parameters
KeyedTraversable<Tk,
T> $inputs
- TheKeyedTraversable
of keys and values to map.(function(Tk, T): Awaitable<bool>) $callable
- The callable containing theAwaitable
operation to apply to$inputs
.
Returns
Awaitable<Vector<ResultOrExceptionWrapper<T>>>
- AnAwaitable
ofVector
ofResultOrExceptionWrapper
after the filtering operation has been applied to the keys and values in$inputs
.
Examples
<<__EntryPoint>>
async function basic_usage_main(): Awaitable<void> {
// Return all non-negative odd numbers
// Positive evens and odds at every third index filtered out,
// Negatives and zero cause exception
$odds = await \HH\Asio\vfkw(
Vector {-1, 0, 1, 2, 3, 4, 5},
async ($idx, $val) ==> {
if ($val <= 0) {
throw new \Exception("$val is non-positive");
} else {
return ($idx % 3) && ($val % 2);
}
},
);
foreach ($odds as $result) {
if ($result->isSucceeded()) {
echo "Success: ";
\var_dump($result->getResult());
} else {
echo "Failed: ";
\var_dump($result->getException()->getMessage());
}
}
}