rosavox/lib/alveolata/pod/functions.php
2025-05-23 07:33:29 +00:00

124 lines
1.9 KiB
PHP

<?php
namespace alveolata\pod;
/**
* @template TypeValue
* @author Christian Fraß <frass@greenscale.de>
*/
class struct_pod
{
/**
* @var bool
*/
public $full;
/**
* @var null|TypeValue
*/
public $value;
/**
* @param bool $full
* @param null|TypeValue $value
*/
public function __construct(
bool $full,
$value
)
{
$this->full = $full;
$this->value = $value;
}
}
/**
* @template TypeValue
* @return struct_pod<TypeValue>
* @author Christian Fraß <frass@greenscale.de>
*/
function make_toom(
) : struct_pod
{
return (new struct_pod(false, null));
}
/**
* @template TypeValue
* @param TypeValue $value
* @return struct_pod<TypeValue>
* @author Christian Fraß <frass@greenscale.de>
*/
function make_full(
$value
) : struct_pod
{
return (new struct_pod(true, $value));
}
/**
* @template TypeValue
* @param struct_pod<TypeValue>
* @return bool
* @author Christian Fraß <frass@greenscale.de>
*/
function has(
struct_pod $pod
): bool
{
return $pod->full;
}
/**
* @template TypeValue
* @param struct_pod<TypeValue>
* @return TypeValue
* @author Christian Fraß <frass@greenscale.de>
*/
function get(
struct_pod $pod
)
{
if (! $pod->full) {
throw (new \Exception('empty'));
}
else {
return $pod->value;
}
}
/**
* creates a pod on base of the input pod; i.e. if the input pod is empty, the output put is too; if the input pod is
* full, the function is applied to its value to make up a new full pod with the result value
*
* @template TypeValueFrom
* @template TypeValueTo
* @param struct_pod<TypeValueFrom>
* @param \Closure {function<TypeValueFrom,TypeValueTo>}
* @return struct_pod<TypeValueTo>
* @author Christian Fraß <frass@greenscale.de>
*/
function brook(
struct_pod $pod,
\Closure $function
) : struct_pod
{
if (! is_something($pod)) {
return make_empty();
}
else {
return make_full($function($pod->value));
}
}
?>