Improve signatures
authorwidmogrod <widmogrod@gmail.com>
Sun, 25 Jun 2017 20:40:12 +0000 (22:40 +0200)
committerwidmogrod <widmogrod@gmail.com>
Sun, 25 Jun 2017 20:40:12 +0000 (22:40 +0200)
example/FreeMonadTest.php
src/Monad/Free/functions.php

index cdb3add..b8d7184 100644 (file)
@@ -7,6 +7,7 @@ use Widmogrod\Monad\State;
 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
@@ -116,7 +117,7 @@ class FreeMonadTest extends \PHPUnit_Framework_TestCase
     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, []);
 
@@ -131,7 +132,7 @@ class FreeMonadTest extends \PHPUnit_Framework_TestCase
     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
index 14b944f..1880067 100644 (file)
@@ -2,14 +2,33 @@
 
 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());
+}