Last active
August 29, 2015 14:06
-
-
Save phuedx/f5b71d7feb1e8e2f796c to your computer and use it in GitHub Desktop.
The Identity 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
<?hh | |
type F1<Ta, Tb> = (function(Ta): Tb); | |
class Identity<Ta> { | |
public function __construct(private Ta $value) {} | |
public function bind<Tb>(F1<Ta, Identity<Tb>> $f): Identity<Tb> { | |
return $f($this->value); | |
} | |
public function run(): Ta { | |
return $this->value; | |
} | |
} | |
function identity<T>(T $value) { | |
return new Identity /* <T> */($value); | |
} | |
$three = identity(1)->bind( | |
$x ==> identity(2)->bind( | |
$y ==> identity($x + $y) | |
) | |
); | |
echo $three->run() . "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now with added
No errors!
.