Coder Social home page Coder Social logo

union.php's Introduction

baethon/union

Provides utilities to define tagged unions.

Tagged unions?

I'm not good with words, yet folks from Folktale describe it nicely:

Modelling data is important for a range of reasons. From performance to correctness to safety. Tagged unions give you a way of modelling choices that forces the correct handling of them, unlike predicate-based branching, such as the one used by if statements and other common control flow structures.

Installation

composer require baethon/union

Usage

To create a tag union it's required to create a class which extends Baethon\Union\AbstractUnion.

class Maybe extends \Baethon\Union\AbstractUnion
{
}

Also you need to define tags which will represent state of the union.

class Maybe extends \Baethon\Union\AbstractUnion
{
	const SOME = 'Some:x';

	const NONE = 'None';
}

Signatures

Each tag has a definition (called signature), which can be defined using following syntax:

{Name}[:[param1[, param2[, ...[, paramN]]]]]

Parameters define whether a tag will hold any value(s) (called arguments). They're optional.

Working with unions

Union can be invoked using static constructor:

$some = Maybe::Some(1);

To work with the state of the union you should use matchWith(). It will return the value returned by the matching callback:

function addTen(Maybe $maybe) {
	return $maybe->matchWith([
		'Some' => function ($x) {
			return $x + 10;
		},
		'None' => function () {
			throw new \Exception('Sorry, can\'t add a number to nothing');
		}
	]);
}

addTen($some); // 11

matchWith() will check if all possible branches are mapped:

  • if a tag is not covered by map UnderflowException will be thrown
  • if map covers more tags than defined ones it will throw 'OverflowException'.

It's possible to use wildcard map to cover all other cases:

$some->matchWith([
	'*' => function () {
		return 100;
	}
]); // 100

You can use defined const values in mapping:

$some->matchWith([
	Maybe::SOME => function () {},
	Maybe::NONE => function () {}
]);

Testing

./vendor/bin/phpunit

union.php's People

Contributors

radmen avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.