1. Go to this page and download the library: Download widmogrod/php-functional library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Widmogrod\Functional as f;
use Widmogrod\Monad\Either;
function read($file)
{
return is_file($file)
? Either\Right::of(file_get_contents($file))
: Either\Left::of(sprintf('File "%s" does not exists', $file));
}
$concat = f\liftM2(
read(__DIR__ . '/e1.php'),
read('aaa'),
function ($first, $second) {
return $first . $second;
}
);
assert($concat instanceof Either\Left);
assert($concat->extract() === 'File "aaa" does not exists');
use Widmogrod\Monad\IO as IO;
use Widmogrod\Functional as f;
// $readFromInput :: Monad a -> IO ()
$readFromInput = f\mcompose(IO\putStrLn, IO\getLine, IO\putStrLn);
$readFromInput(Monad\Identity::of('Enter something and press <enter>'))->run();
use Widmogrod\Monad\Writer as W;
use Widmogrod\Functional as f;
use Widmogrod\Primitive\Stringg as S;
$data = [1, 10, 15, 20, 25];
$filter = function($i) {
if ($i % 2 == 1) {
return W::of(false, S::of("Reject odd number $i.\n"));
} else if($i > 15) {
return W::of(false, S::of("Reject $i because it is bigger than 15\n"));
}
return W::of(true);
};
list($result, $log) = f\filterM($filter, $data)->runWriter();
use Widmogrod\Monad\Reader as R;
use Widmogrod\Functional as f;
function hello($name) {
return "Hello $name!";
}
function ask($content)
{
return R::of(function($name) use($content) {
return $content.
($name == 'World' ? '' : ' How are you?');
});
}
$r = R\reader('hello')
->bind('ask')
->map('strtoupper');
assert($r->runReader('World') === 'HELLO WORLD!')
assert($r->runReader('World') === 'HELLO GILLES! HOW ARE YOU?')
$state = [
'productsCount' => 0,
'products' => [],
];
$scenario =
Given('Product in cart', $state)
->When("I add product 'coca-cola'")
->When("I add product 'milk'")
->Then("The number of products is '2'");
$result = $scenario->Run([
"/^I add product '(.*)'/" => function ($state, $productName) {
$state['productsCount'] += 1;
$state['products'][] = $productName;
return $state;
},
], [
"/^The number of products is '(\d+)'/" => function ($state, int $expected) {
return $state['productsCount'] === $expected;
},
]);