Add contains, exists, toArray

- Update docs
This commit is contained in:
2024-07-29 13:58:33 -04:00
parent 57af645d87
commit fad428a4e4
6 changed files with 262 additions and 76 deletions

View File

@@ -10,6 +10,7 @@ declare(strict_types=1);
namespace BitBadger\InspiredByFSharp;
use Error;
use Exception;
use InvalidArgumentException;
/**
@@ -84,15 +85,26 @@ readonly class Option
/**
* Get the value, or return the value of a callable function
*
* @template U The return type of the callable provided
* @param callable(): U $f The callable function to use for `None` options
* @return T|mixed The value if `Some`, the result of the callable if `None`
* @template TCalled The return type of the callable provided
* @param callable(): TCalled $f The callable function to use for `None` options
* @return T|TCalled The value if `Some`, the result of the callable if `None`
*/
public function getOrCall(callable $f): mixed
{
return $this->value ?? $f();
}
/**
* Get the value, or throw the
* @param callable(): Exception $exFunc A function to construct the exception to throw
* @return T The value of the option if `Some`
* @throws Exception If the option is `None`
*/
public function getOrThrow(callable $exFunc): mixed
{
return $this->value ?? throw $exFunc();
}
/**
* Bind a function to this option (railway processing)
*
@@ -108,12 +120,38 @@ readonly class Option
return $this->isNone() ? $this : $f($this->get());
}
/**
* Does the option contain the given value?
*
* @param T $value The value to be checked
* @param bool $strict True for strict equality (`===`), false for loose equality (`==`); optional, default is true
* @return bool True if the value matches, false if not; `None` always returns false
*/
public function contains(mixed $value, bool $strict = true): bool
{
return match (true) {
$this->isNone() => false,
default => $strict ? $this->value === $value : $this->value == $value,
};
}
/**
* Does the value of the option match the given predicate function?
*
* @param callable(T): bool $f The function to determine whether the value matches
* @return bool True if the `Some` value matches the function, false otherwise
*/
public function exists(callable $f): bool
{
return $this->isSome() ? $f($this->value) : false;
}
/**
* Map this optional value to another value
*
* @template U The type of the mapping function
* @param callable(T): U $f The mapping function
* @return Option<U> A `Some` instance with the transformed value if `Some`, `None` otherwise
* @template TMapped The type of the mapping function
* @param callable(T): TMapped $f The mapping function
* @return Option<TMapped> A `Some` instance with the transformed value if `Some`, `None` otherwise
*/
public function map(callable $f): self
{
@@ -140,25 +178,7 @@ readonly class Option
*/
public function filter(callable $f): self
{
return match (true) {
$this->isNone() => $this,
default => $f($this->value) ? $this : self::None(),
};
}
/**
* Does the option have the given value?
*
* @param T $value The value to be checked
* @param bool $strict True for strict equality (`===`), false for loose equality (`==`)
* @return bool True if the value matches, false if not; `None` always returns false
*/
public function is(mixed $value, bool $strict = true): bool
{
return match (true) {
$this->isNone() => false,
default => $strict ? $this->value === $value : $this->value == $value,
};
return $this->isNone() || $this->exists($f) ? $this : self::None();
}
/**
@@ -183,6 +203,16 @@ readonly class Option
return $this;
}
/**
* Convert this option into a 0 or 1 item array
*
* @return T[] An empty array for `None`, a 1-item array for `Some`
*/
public function toArray(): array
{
return $this->isSome() ? [$this->value] : [];
}
/**
* Convert this to a PhpOption option
*

View File

@@ -103,12 +103,38 @@ readonly class Result
return $this->isError() ? $this : $f($this->getOK());
}
/**
* Does this result's "OK" value match the given value?
*
* @param TOK $value The value to be matched
* @param bool $strict True for strict equality (`===`), false for loose equality (`==`); optional, default is true
* @return bool True if the "OK" value matches the one provided, false otherwise
*/
public function contains(mixed $value, bool $strict = true): bool
{
return match (true) {
$this->isError() => false,
default => $this->okValue->contains($value, $strict),
};
}
/**
* Does the "OK" value of this result match the given predicate function?
*
* @param callable(TOK): bool $f The function to determine whether the value matches
* @return bool True if the OK value matches the function, false otherwise
*/
public function exists(callable $f): bool
{
return $this->isOK() ? $f($this->okValue->get()) : false;
}
/**
* 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
* @template TMapped The type of the mapping function
* @param callable(TOK): TMapped $f The mapping function
* @return Result<TMapped, TError> A transformed `OK` instance or the original `Error` instance
*/
public function map(callable $f): self
{
@@ -118,9 +144,9 @@ readonly class Result
/**
* 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
* @template TMapped The type of the mapping function
* @param callable(TError): TMapped $f The mapping function
* @return Result<TOK, TMapped> A transformed `Error` instance or the original `OK` instance
*/
public function mapError(callable $f): self
{
@@ -139,6 +165,16 @@ readonly class Result
}
}
/**
* Convert this result into a 0 or 1 item array
*
* @return TOK[] An empty array for `Error`, a 1-item array for `OK`
*/
public function toArray(): array
{
return $this->okValue->toArray();
}
/**
* Transform a `Result`'s `OK` value to an `Option`
*