diff --git a/examples/namespaced-fn.php b/examples/namespaced-fn.php new file mode 100644 index 0000000..9429115 --- /dev/null +++ b/examples/namespaced-fn.php @@ -0,0 +1,26 @@ + myFunc(...); // exception, see below + print $result . "\n"; // works + + /** +Fatal error: Uncaught Error: Call to undefined function myFunc() in /home/thgs/Projects/Functional-PHP/functional/examples/namespaced-fn.php:15 +Stack trace: +#0 {main} + thrown in /home/thgs/Projects/Functional-PHP/functional/examples/namespaced-fn.php on line 15 + + I am at d5a5dc8d (func-composition) + */ +} diff --git a/src/Data/IO.php b/src/Data/IO.php index bc0c28f..0f38430 100644 --- a/src/Data/IO.php +++ b/src/Data/IO.php @@ -85,8 +85,7 @@ public function fmap(callable $f): FunctorInstance // todo: self or static? return new self( function () use ($f) { - $result = ($this->action)(); - return $f($result); + return ($this->action)() |> $f; } ); } @@ -116,11 +115,10 @@ public function sequence(ApplicativeInstance $fa): ApplicativeInstance } // todo: rewrite this in a more concise manner, but for brevity now: - $do = function () use ($fa) { - $f = $this(); - $g = $fa(); - return partial ($f) ($g); - }; + $do = fn () + => null |> $fa + |> partial (null |> $this); + return IO::inject($do); } @@ -149,10 +147,8 @@ public function bind(callable $f): MonadInstance { $action = $this->action; $do = function () use ($f, $action) { - $x = ($action)(); - // todo: could add a type check here? that return type is indeed m b ? - return (partial ($f) ($x)) + return (null |> $action |> partial ($f)) ->getValue(); // unIO }; return new IO($do); diff --git a/src/functions.php b/src/functions.php index 48198d5..549143a 100644 --- a/src/functions.php +++ b/src/functions.php @@ -147,7 +147,7 @@ function dn(MonadInstance $ma, callable ...$fs) { $last = $ma; foreach ($fs as $new) { - $last = $last->bind($new); + $last = $new |> $last->bind(...); // Pedantic type check below if (!$last instanceof MonadInstance) {