- var_dump: documentation ( source)
- array_merge: documentation ( source)
- array_rand: documentation ( source)
<?php
class Past {
private $value;
private $pastValues;
function __construct($value, array $pastValues = []) {
$this->value = $value;
$this->pastValues = $pastValues;
}
function map(callable $fn) {
return $this->update($fn($this->value));
}
function update($value) {
return new Past($value, array_merge($this->pastValues, [$this->value]));
}
function get() {
return array_rand($this->pastValues);
}
}
$answer = new Past(42);
$answer = $answer->map(function ($number) { return $number + 1; });
$answer = $answer->update(-3);
$answer = $answer->map(function ($number) { return $number * -1; });
$answer = $answer->map(function ($number) { return $number * 3; });
$answer = $answer->update('completely new value, hehehe');
var_dump($answer->get());