PHP code example of haskellcamargo / php-maybe-monad
1. Go to this page and download the library: Download haskellcamargo/php-maybe-monad 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/ */
haskellcamargo / php-maybe-monad example snippets
use HaskellCamargo\Maybe;
Maybe\Maybe(@$_GET["username"])->bind(function($user)) {
echo "Welcome, $user. You're logged in!";
});
$userAge = Maybe\Maybe(null)->fromMaybe(0); // => 0
$userAge = Maybe\Maybe(19)->fromMaybe(0); // => 19
$age = Maybe\Maybe(null)->bind(function($x) {
return 10;
}); // => Nothing
$age = Maybe\Maybe(10)
->bind(function($x) {
return $x + 10; // => Just(20);
})
->bind(function($x) {
return $x + 20; // => Just(40);
})->fromJust(); // => 40
Maybe\Maybe("Foo")->fromJust(); // => "Foo"
Maybe\Maybe(null)->fromJust(); // => Exception: Cannot cal fromJust() on Nothing
Maybe\Maybe(10)->fromMaybe(5); // => 10
Maybe\Maybe(null)->fromMaybe(5); // => 5
Maybe\Maybe(10)->isJust(); // => true
Maybe\Maybe(null)->isJust(); // => false
Maybe\Maybe(10)->isNothing(); // => false
Maybe\Maybe(null)->isNothing(); // => true
$just = Maybe\Maybe(10);
$nothing = Maybe\Maybe(null);
$just->maybe(40, function($num) {
return $num + 15;
}); // => 25
$nothing->maybe(40, function($num) {
return $num + 15;
}); // => 40
Maybe\Maybe(10)->toList(); // => [10]
Maybe\Maybe(null)->toList(); // => []
bash
composer