Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions examples/namespaced-fn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace a\b\c {
function myFunc($x) {
return $x + 123;
}
}

namespace {
use function a\b\c\myFunc;

$result = myFunc(123);
print $result . "\n"; // works

$result = 123 |> 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)
*/
}
16 changes: 6 additions & 10 deletions src/Data/IO.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
);
}
Expand Down Expand Up @@ -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);
Comment on lines +118 to +120
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would look better like

Suggested change
$do = fn ()
=> null |> $fa
|> partial (null |> $this);
$do = fn () => $fa() |> partial ($this());


return IO::inject($do);
}

Expand Down Expand Up @@ -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))
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return (null |> $action |> partial ($f))
return ($action() |> partial ($f))

->getValue(); // unIO
};
return new IO($do);
Expand Down
2 changes: 1 addition & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down