use function Widmogrod\Functional\append;
use function Widmogrod\Functional\match;
use function Widmogrod\Monad\Free\liftF;
+use function Widmogrod\Monad\Free\runFree;
use function Widmogrod\Monad\IO\getLine;
interface TeletypeF
public function test_it_should_allow_to_interpret_as_a_state_monad()
{
$echo = echo_();
- $result = $echo->runFree(interpretState);
+ $result = runFree(interpretState, $echo);
$this->assertInstanceOf(State::class, $result);
$result = State\execState($result, []);
public function test_it_should_allow_to_interpret_as_IO()
{
$echo = echo_();
- $result = $echo->runFree(interpretIO);
+ $result = runFree(interpretIO, $echo);
$this->assertInstanceOf(IO::class, $result);
// Since in PHPUnit STDIN is closed
// this run will not work, but serves as an example
namespace Widmogrod\Monad\Free;
+use Widmogrod\Functional as f;
+
+const liftF = 'Widmogrod\Monad\Free\liftF';
+
/**
- * liftF :: (Functor f, Monad m) => f a -> FreeT f m a
- *
- * @param $value
+ * @param mixed $value
*
- * @return mixed|Free
+ * @return Free
*/
function liftF($value)
{
return Free::of(Pure::of, $value);
}
+
+const runFree = 'Widmogrod\Monad\Free\runFree';
+
+/**
+ * runFree :: Monad m => (a -> m b) -> MonadFree f a -> m b
+ *
+ * @param callable $interpretation Monad m => (a -> m b) -> m b
+ * @param MonadFree $free
+ *
+ * @return mixed
+ */
+function runFree(callable $interpretation, MonadFree $free = null)
+{
+ return call_user_func_array(f\curryN(2, function (callable $interpretation, MonadFree $free) {
+ return $free->runFree($interpretation);
+ }), func_get_args());
+}