Created
October 24, 2014 19:43
-
-
Save igorw/b0a213dfb9247048a09a to your computer and use it in GitHub Desktop.
The Past Monad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace igorw; | |
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()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Temporal paradoxes be dammed!