Add tap for Option and Result

This commit is contained in:
2024-07-27 13:03:54 -04:00
parent 193147cfb3
commit 7837f4af17
4 changed files with 72 additions and 0 deletions

View File

@@ -202,4 +202,17 @@ readonly class Option
{
return self::isSome($it) ? $it->get() : null;
}
/**
* Tap into the `Result` for a secondary action, returning the result
*
* @param callable(Option<T>): mixed $f The function to run (return value is ignored)
* @param Option<T> $it The option against which the function should be run
* @return Option<T> The same option provided
*/
public static function tap(callable $f, Option $it): Option
{
$f($it);
return $it;
}
}

View File

@@ -165,4 +165,17 @@ readonly class Result
{
return Result::isOK($it) ? Option::Some($it->getOK()) : Option::None();
}
/**
* Tap into the `Result` for a secondary action, returning the result
*
* @param callable(Result<TOK, TError>): mixed $f The function to run (return value is ignored)
* @param Result<TOK, TError> $it The result against which the function should be run
* @return Result<TOK, TError> The same result provided
*/
public static function tap(callable $f, Result $it): Result
{
$f($it);
return $it;
}
}