php-fp / php-fp-maybe Goto Github PK
View Code? Open in Web Editor NEWAn implementation of the Maybe monad in PHP.
License: MIT License
An implementation of the Maybe monad in PHP.
License: MIT License
This package requires PHP 7:
{
"require": {
"php": ">=7.0"
}
}
This is broader than just Maybe, but I'm not sure if you can create issues for a whole group. Anyhow, I've been working on an implementation of Task, and found myself wanting a liftA2()
function. It struck me that there's at least three ways to approach how I declare the function signature:
function liftA2(callable $f, Task $a1, Task $a2) {
return $a1->map(function ($a1Val) use ($f) {
return function($x) use ($a1Val) {
return $f($a1Val, $x);
}
})->ap($a2);
}
function liftA2(callable $f, $a1, $a2) {
return $a1->map(function ($a1Val) use ($f) {
return function($x) use ($a1Val) {
return $f($a1Val, $x);
}
})->ap($a2);
}
function liftA2(callable $f, Functor $a1, Applicative $a2) {
return $a1->map(function ($a1Val) use ($f) {
return function($x) use ($a1Val) {
return $f($a1Val, $x);
}
})->ap($a2);
}
Option 1 works, but isn't terribly flexible. Option 2 also works, until someone tries to pass in something without a ->ap()
or ->map()
method. I could manually check for the given method with reflection, but it seems a pain. Option 3 would mean that PHP did the checking for me, and would allow me to re-use liftA2()
across different types. But it would also mean that I have to make sure that all the types I want to use it with implement that interface.
I haven't actually tried it, so I don't know if there's good reasons not to. Is this something you've thought about?
This code is impossible because Maybe
is an abstract class. Attempting to run it results in a fatal error.
Is Maybe
missing a constructor that returns Just
for a value, or Nothing
for a null? From what I can tell, Haskell's Maybe
constructor does a type check and returns a Just
when given a value and Nothing
otherwise. Would it make sense to have the following?
$maybe = Maybe::of(null);
assert($maybe instanceof Nothing);
$maybe = Maybe::of(1);
assert($maybe instanceof Just);
Or, if the violates the Applicative constructor, perhaps the following?
$maybe = Maybe::maybe(null); // Nothing
$maybe = Maybe::maybe(1); // Just
$maybe = Maybe::maybe(false); // Just
Currently, the tests are only running because no tests have any dependencies under PhpFp\Maybe\Test
, and PHPUnit doesn't let poor autoloading get in its way. There needs to be a separate Composer entry, for autoload-dev
, to load the tests namespace.
I forgot to do this in my sweep! The examples should be in line with the coding standard.
Would it make sense to have a Maybe::toEither(): Either
method?
abstract class Maybe
{
// ...
public function toEither(): Either
{
return $this->map('\PhpFp\Either\Either::of')->fork(Either::left(null));
}
}
Or perhaps it would be better to have abstract public function toEither(): Either
implemented as:
// Just
public function toEither(): Either
{
return Either::of($this->value);
}
// Nothing
public function toEither(): Either
{
return Either::left(null);
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.