The Wayback Machine - https://web.archive.org/web/20210120171541/https://github.com/mckayb/phantasy-recursion-schemes
Skip to content
master
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Phantasy Recursion Schemes CircleCICoverage Status

Common recursion schemes implemented in PHP.

Getting Started

Installation

composer require mckayb/phantasy-recursion-schemes

Usage

// 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

For more information, read the docs!

What's Included

  • Catamorphisms, Anamorphisms, Hylomorphisms

Contributing

Find a bug? Want to make any additions? Just create an issue or open up a pull request.

Want more?

For other helpers not included in this repo, check out

You can’t perform that action at this time.