定制 mckayb/phantasy-types 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mckayb/phantasy-types

Composer 安装命令:

composer require mckayb/phantasy-types

包简介

Sum Types and Product Types in PHP

README 文档

README

Library for creating Sum Types and Product Types in PHP

Getting Started

Installation

composer require mckayb/phantasy-types

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.

Inspiration

统计信息

  • 总下载量: 23
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-07-03

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固