Monads/Writer monad: Difference between revisions

Content added Content deleted
mNo edit summary
(Add php version)
Line 731: Line 731:
+ 1 → 3.23607
+ 1 → 3.23607
/ 2 → 1.61803
/ 2 → 1.61803
</pre>

=={{header|PHP}}==
<lang php>class WriterMonad {

/** @var mixed */
private $value;
/** @var string[] */
private $logs;

private function __construct($value, array $logs = []) {
$this->value = $value;
$this->logs = $logs;
}

public static function unit($value, string $log): WriterMonad {
return new WriterMonad($value, ["{$log}: {$value}"]);
}

public function bind(callable $mapper): WriterMonad {
$mapped = $mapper($this->value);
assert($mapped instanceof WriterMonad);
return new WriterMonad($mapped->value, [...$this->logs, ...$mapped->logs]);
}

public function value() {
return $this->value;
}

public function logs(): array {
return $this->logs;
}
}

$root = fn(float $i): float => sqrt($i);
$addOne = fn(float $i): float => $i + 1;
$half = fn(float $i): float => $i / 2;

$m = fn (callable $callback, string $log): callable => fn ($value): WriterMonad => WriterMonad::unit($callback($value), $log);

$result = WriterMonad::unit(5, "Initial value")
->bind($m($root, "square root"))
->bind($m($addOne, "add one"))
->bind($m($half, "half"));

print "The Golden Ratio is: {$result->value()}\n";
print join("\n", $result->logs());</lang>

{{out}}
<pre>
The Golden Ratio is: 1.6180339887499
Initial value: 5
square root: 2.2360679774998
add one: 3.2360679774998
half: 1.6180339887499
</pre>
</pre>