定制 yshnb/php-monadic 二次开发

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

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

yshnb/php-monadic

Composer 安装命令:

composer require yshnb/php-monadic

包简介

Monadic type programming in PHP

README 文档

README

library for monad pattern in PHP.

type

this library implements below type (class)

  • Identity
  • Maybe
  • Either
  • ListLike(List)

Usage

Install

you can install from packagist.org.

composer require yshnb/php-monadic:dev-master

Examples

Identity

Identity is a monad type, which simply wrap any value.

<?php

use Monadic\Type\Identity;

$foo = 1;
$identity = Identity::unit($foo)->bind(function($val) {
    return Identity::unit($val * 2);
});
echo $identity->get(); // 2

Maybe

<?php

use Monadic\Type\Maybe;

$foo = 1;
$maybe = Maybe::unit($foo)->bind(function($val) {
    return Maybe::unit($val + 1);
});
echo $maybe->get(); // 2

$maybe = $maybe->bind(function($val) {
    return Maybe::unit();
});
echo $maybe->get(); // null

Either

<?php

use Monadic\Type\Either\Right;
use Monadic\Type\Either\Left;

$foo = 1;
$either = Right::unit($foo)->bind(function($val) {
    return Left::unit($val + 1);
})->left(function($val) {
    return Right::unit($val + 1);
})->left(function($val) {
    // not executed
    return Right::unit($val + 1);
});
echo $either->get(); // 3

ListLike

ListLike represents list type (class). Because list is a reserved word in PHP, this type is called ListLike.

<?php

use Monadic\Type\ListLike;

$listLike = ListLike::unit(1,2,3);
$listLike = $listLike->bind(function($val) {
    return ListLike::unit($val * 2);
});
echo $listLike[0]; // 3
echo $listLike[1]; // 4
echo $listLike[2]; // 6

统计信息

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

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-06-21