2017 © Pedro Peláez
 

library phantasy-types

Sum Types and Product Types in PHP

image

mckayb/phantasy-types

Sum Types and Product Types in PHP

  • Thursday, February 15, 2018
  • by mckayb
  • Repository
  • 1 Watchers
  • 1 Stars
  • 12 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

Phantasy Types CircleCICoverage Status

Library for creating Sum Types and Product Types in PHP, (*1)

Getting Started

Installation

composer require mckayb/phantasy-types, (*2)

Usage

Product Types

use function Phantasy\Types\product;

$Point3D = product('Point3D', ['x', 'y', 'z']);
echo $Point3D; // 'Point3D'

$a = $Point3D(1, 2, 3);

echo $a; // 'Point3D(1, 2, 3)'
$Point3D->scale = function ($n) {
    return $this->Point3D($n * $this->x, $n * $this->y, $n * $this->z);
};

/*
Could also do
$Point3D->scale = function ($n) use ($Point3D) {
    return $Point3D($n * $this->x, $n * $this->y, $n * $this->z);
};
*/

$b = $a->scale(2);
echo $b; // 'Point3D(2, 4, 6)'

Sum Types

use function Phantasy\Types\sum;

$Option = sum('Option', [
    'Some' => ['x'],
    'None' => []
]);

$a = $Option->Some(1);
$b = $Option->None();

echo $a; // "Option.Some(1)"
echo $b; // "Option.None()"

$Option->map = function ($f) {
    return $this->cata([
        'Some' => function ($x) use ($f) {
            return $this->Some($f($x));
        },
        'None' => function () {
            return $this->None();
        }
    ]);
};

$c = $a->map(function ($x) {
    return $x + 1;
});

$d = $b->map(function ($x) {
    return $x + 1;
});

echo $c; // "Option.Some(2)"
echo $d; // "Option.None()"

Contributing

Find a bug? Want to make any additions? Just create an issue or open up a pull request., (*3)

Inspiration

The Versions

15/02 2018

dev-master

9999999-dev

Sum Types and Product Types in PHP

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

15/02 2018

v0.3.0

0.3.0.0

Sum Types and Product Types in PHP

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

28/09 2017

dev-feature/make-fields-accessible-to-sum-types

dev-feature/make-fields-accessible-to-sum-types

Sum Types and Product Types in PHP

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

28/09 2017

v0.2.0

0.2.0.0

Sum Types and Product Types in PHP

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

04/07 2017

v0.1.1

0.1.1.0

Sum Types and Product Types in PHP

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

04/07 2017

v0.1.0

0.1.0.0

Sum Types and Product Types in PHP

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires