承接 haskellcamargo/php-maybe-monad 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

haskellcamargo/php-maybe-monad

Composer 安装命令:

composer require haskellcamargo/php-maybe-monad

包简介

A PHP implementation of Haskell's Maybe monad

README 文档

README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

To deal with computations that may fail. A port of Haskell's Data.Maybe module for PHP > 5.4.

Install

You can install via Composer.

composer require haskellcamargo/php-maybe-monad

Example

use HaskellCamargo\Maybe;

Maybe\Maybe(@$_GET["username"])->bind(function($user)) {
  echo "Welcome, $user. You're logged in!";
});

$userAge = Maybe\Maybe(null)->fromMaybe(0); // => 0
$userAge = Maybe\Maybe(19)->fromMaybe(0); // => 19

Documentation

A Maybe type encapsulates an optional value. A value of type Maybe a either contains a value of type a (represented as Just a), or it is empty (represented as Nothing). Using Maybe is a good way to deal with errors or exceptional cases without resorting to drastic measures such as Exception. The Maybe type is also a monad. It is a simple kind of error monad, where all errors are represented by Nothing. A richer error monad can be built using the Either type.

bind :: (Maybe a, callable) -> Maybe b

Equivalent to Haskell's >>= operator. Its first argument is a value in a monadic type, its second argument is a function that maps from the underlying type of the first argument to another monadic type, and its results is in that other monadic type.

$age = Maybe\Maybe(null)->bind(function($x) {
  return 10;
}); // => Nothing

$age = Maybe\Maybe(10)
->bind(function($x) {
  return $x + 10; // => Just(20);
})
->bind(function($x) {
  return $x + 20; // => Just(40);
})->fromJust(); // => 40

fromJust :: Maybe a -> a

Extracts the element out of a Just and returns an error if its argument is Nothing.

Maybe\Maybe("Foo")->fromJust(); // => "Foo"
Maybe\Maybe(null)->fromJust(); // => Exception: Cannot cal fromJust() on Nothing

fromMaybe :: (Maybe a, a) -> a

Takes a Maybe value and a default value. If the Maybe is Nothing, it returns the default values; otherwise, it returns the value contained in the Maybe.

Maybe\Maybe(10)->fromMaybe(5); // => 10
Maybe\Maybe(null)->fromMaybe(5); // => 5

isJust :: Maybe a -> boolean

Returns true if its argument is of the form Just _.

Maybe\Maybe(10)->isJust(); // => true
Maybe\Maybe(null)->isJust(); // => false

isNothing :: Maybe a -> boolean

Returns true if its argument is of the form Nothing.

Maybe\Maybe(10)->isNothing(); // => false
Maybe\Maybe(null)->isNothing(); // => true

maybe :: (Maybe a, b, callable) -> b

Takes a default value, a function and, of course, a Maybe value. If the Maybe value is Nothing, the function returns the default value. Otherwise, it applies the function to the value inside the Just and returns the result.

$just = Maybe\Maybe(10);
$nothing = Maybe\Maybe(null);

$just->maybe(40, function($num) {
        return $num + 15;
}); // => 25

$nothing->maybe(40, function($num) {
  return $num + 15;
}); // => 40

toList :: Maybe a -> array

Returns an empty list when given Nothing or a singleton list when not given Nothing.

Maybe\Maybe(10)->toList(); // => [10]
Maybe\Maybe(null)->toList(); // => []

Made with ❤️ by Marcelo Camargo and Reinaldo Rauch

License

MIT

haskellcamargo/php-maybe-monad 适用场景与选型建议

haskellcamargo/php-maybe-monad 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.19k 次下载、GitHub Stars 达 29, 最近一次更新时间为 2015 年 06 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 haskellcamargo/php-maybe-monad 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 haskellcamargo/php-maybe-monad 我们能提供哪些服务?
定制开发 / 二次开发

基于 haskellcamargo/php-maybe-monad 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.19k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 29
  • 点击次数: 18
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 29
  • Watchers: 3
  • Forks: 6
  • 开发语言: PHP

其他信息

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