Skip to content

Instantly share code, notes, and snippets.

@phuedx
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phuedx/f5b71d7feb1e8e2f796c to your computer and use it in GitHub Desktop.
Save phuedx/f5b71d7feb1e8e2f796c to your computer and use it in GitHub Desktop.
The Identity monad.
<?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";
@phuedx
Copy link
Author

phuedx commented Sep 23, 2014

Now with added No errors!.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment