Add toPhpOption; convert both to mostly non-static

This commit is contained in:
2024-07-28 14:15:24 -04:00
parent 7d25b9ea28
commit 5a8a41a660
5 changed files with 581 additions and 567 deletions

View File

@@ -66,6 +66,84 @@ readonly class Result
return $this->errorValue->get();
}
/**
* Is this result `OK`?
*
* @return bool True if the result is `OK`, false if it is `Error`
*/
public function isOK(): bool
{
return $this->okValue->isSome();
}
/**
* Is this result `Error`?
*
* @return bool True if the result is `Error`, false if it is `OK`
*/
public function isError(): bool
{
return $this->errorValue->isSome();
}
/**
* Map an `OK` result to another, leaving an `Error` result unmodified
*
* @template U The type of the mapping function
* @param callable(TOK): U $f The mapping function
* @return Result<U, TError> A transformed `OK` instance or the original `Error` instance
*/
public function map(callable $f): self
{
return $this->isOK() ? self::OK($f($this->getOK())) : $this;
}
/**
* Map an `Error` result to another, leaving an `OK` result unmodified
*
* @template U The type of the mapping function
* @param callable(TError): U $f The mapping function
* @return Result<TOK, U> A transformed `Error` instance or the original `OK` instance
*/
public function mapError(callable $f): self
{
return $this->isError() ? self::Error($f($this->getError())) : $this;
}
/**
* Execute a function on an `OK` value (if it exists)
*
* @param callable(TOK): void $f The function to call
*/
public function iter(callable $f): void
{
if ($this->isOK()) {
$f($this->getOK());
}
}
/**
* Transform a `Result`'s `OK` value to an `Option`
*
* @return Option<TOK> A `Some` option with the OK value if `OK`, `None` if `Error`
*/
public function toOption(): Option
{
return $this->isOK() ? Option::Some($this->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)
* @return Result<TOK, TError> The same result provided
*/
public function tap(callable $f): Result
{
$f($this);
return $this;
}
/**
* Create an `OK` result
*
@@ -93,89 +171,4 @@ readonly class Result
}
return new self(errorValue: $value);
}
/**
* Is the given result `OK`?
*
* @param Result $it The result in question
* @return bool True if the result is `OK`, false if it is `Error`
*/
public static function isOK(Result $it): bool
{
return Option::isSome($it->okValue);
}
/**
* Is the given result `Error`?
*
* @param Result $it The result in question
* @return bool True if the result is `Error`, false if it is `OK`
*/
public static function isError(Result $it): bool
{
return Option::isSome($it->errorValue);
}
/**
* Map an `OK` result to another, leaving an `Error` result unmodified
*
* @template U The type of the mapping function
* @param callable(TOK): U $f The mapping function
* @param Result<TOK, TError> $it The result in question
* @return Result<U, TError> A transformed `OK` instance, or an `Error` instance with the same value
*/
public static function map(callable $f, Result $it): self
{
return self::isOK($it) ? self::OK($f($it->getOK())) : self::Error($it->getError());
}
/**
* Map an `Error` result to another, leaving an `OK` result unmodified
*
* @template U The type of the mapping function
* @param callable(TError): U $f The mapping function
* @param Result<TOK, TError> $it The result in question
* @return Result<TOK, U> A transformed `Error` instance, or an `OK` instance with the same value
*/
public static function mapError(callable $f, Result $it): self
{
return self::isError($it) ? self::Error($f($it->getError())) : self::OK($it->getOK());
}
/**
* Execute a function on an `OK` value (if it exists)
*
* @param callable(TOK): void $f The function to call
* @param Result<TOK, TError> $it The result in question
*/
public static function iter(callable $f, Result $it): void
{
if (self::isOK($it)) {
$f($it->getOK());
}
}
/**
* Transform a `Result`'s `OK` value to an `Option`
*
* @param Result<TOK, TError> $it The result in question
* @return Option<TOK> A `Some` option with the OK value if `OK`, `None` if `Error`
*/
public static function toOption(Result $it): Option
{
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;
}
}