PHP code example of mckayb / phantasy-recursion-schemes

1. Go to this page and download the library: Download mckayb/phantasy-recursion-schemes 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/ */

    

mckayb / phantasy-recursion-schemes example snippets


// This comes from https://github.com/mckayb/phantasy-types
use function Phantasy\Types\sum;
use function Phantasy\Recursion\cata;

$LL = sum('LinkedList', [
	'Cons' => ['head', 'tail'],
	'Nil' => []
]);
$LL->map = function (callable $f) {
	return $this->cata([
		'Cons' => function ($head, $tail) use ($f) {
			return $this->Cons($head, $f($tail));
 		},
		'Nil' => function () {
			return $this->Nil();
		}
	]);
};
$LL->isNil = function () {
	return $this->cata([
		'Cons' => function ($head, $tail) {
			return false;
		},
		'Nil' => function () {
			return true;
		}
	]);
};
$alg = function ($x) {
	return $x->isNil() ? 0 : $x->head + $x->tail;
};
$a = $LL->Cons(3, $LL->Cons(2, $LL->Cons(1, $LL->Nil())));
echo cata($alg, $a);
// 6