Skip to content

Instantly share code, notes, and snippets.

@rightfold
Created May 8, 2016 18:42
Show Gist options
  • Save rightfold/74c1feb5f65ac10a0603f6975ea0dce4 to your computer and use it in GitHub Desktop.
Save rightfold/74c1feb5f65ac10a0603f6975ea0dce4 to your computer and use it in GitHub Desktop.
<?php
return (function() {
$id = function($x) use(&$id) {
return $x;
};
$const = function($x) use(&$const, &$id) {
return function($_) use(&$const, &$id, &$x) {
return $x;
};
};
$compose = function($f) use(&$compose, &$const, &$id) {
return function($g) use(&$compose, &$const, &$f, &$id) {
return function($x) use(&$compose, &$const, &$f, &$g, &$id) {
return ($f)(($g)($x));
};
};
};
$category = (object)[
'compose' => $compose,
'compose-id' => $id,
];
$semigroup = (object)[
'append' => function($f) use(&$category, &$compose, &$const, &$id, &$semigroup) {
return function($g) use(&$category, &$compose, &$const, &$f, &$id, &$semigroup) {
return (($compose)($f))($g);
};
},
];
$monoid = (object)[
'append' => ($semigroup)->append,
'append-id' => $id,
];
return (object)[
'category' => $category,
'compose' => $compose,
'const' => $const,
'id' => $id,
'monoid' => $monoid,
'semigroup' => $semigroup,
];
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment