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;
}
}