承接 react/partial 相关项目开发

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

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

react/partial

Composer 安装命令:

composer require react/partial

包简介

Partial function application.

README 文档

README

Partial function application.

Build Status Code Climate

Install

The recommended way to install react/partial is through composer.

{
    "require": {
        "react/partial": "~2.0"
    }
}

Concept

Partial application (or partial function application) refers to the process of fixing a number of arguments to a function, producing another function of smaller arity. Given a function f:(X x Y x Z) -> N, we might fix (or 'bind') the first argument, producing a function of type f:(Y x Z) -> N. Evaluation of this function might be represented as f partial(2, 3). Note that the result of partial function application in this case is a function that takes two arguments.

Basically, what this allows you to do is pre-fill arguments of a function, which is particularly useful if you don't have control over the function caller.

Let's say you have an async operation which takes a callback. How about a file download. The callback is called with a single argument: The contents of the file. Let's also say that you have a function that you want to be called once that file download completes. This function however needs to know an additional piece of information: the filename.

public function handleDownload($filename)
{
    $this->downloadFile($filename, ...);
}

public function downloadFile($filename, $callback)
{
    $contents = get the darn file asynchronously...
    $callback($contents);
}

public function processDownloadResult($filename, $contents)
{
    echo "The file $filename contained a shitload of stuff:\n";
    echo $contents;
}

The conventional approach to this problem is to wrap everything in a closure like so:

public function handleDownload($filename)
{
    $this->downloadFile($filename, function ($contents) use ($filename) {
        $this->processDownloadResult($filename, $contents);
    });
}

This is not too bad, especially with PHP 5.4, but with 5.3 you need to do the annoying $that = $this dance, and in general it's a lot of verbose boilerplate that you don't really want to litter your code with.

This is where partial application can help. Since we want to pre-fill an argument to the function that will be called, we just call bind, which will insert it to the left of the arguments list. The return value of bind is a new function which takes one $content argument.

use function React\Partial\bind;

public function handleDownload($filename)
{
    $this->downloadFile($filename, bind([$this, 'processDownloadResult'], $filename));
}

Partialing is dependency injection for functions! How awesome is that?

Examples

bind

use function React\Partial\bind;

$add = function ($a, $b) {
    return $a + $b;
};

$addOne = bind($add, 1);

echo sprintf("%d\n", $addOne(5));
// outputs 6

bind_right

use function React\Partial\bind_right;

$div = function ($a, $b, $c) {
    return $a / $b / $c;
};

$divMore = bind_right($div, 20, 10);

echo sprintf("%F\n", $divMore(100)); // 100 / 20 / 10
// outputs 0.5

placeholder

It is possible to use the function (there is an alias called placeholder) to skip some arguments when partially applying.

This allows you to pre-define arguments on the right, and have the left ones bound at call time.

This example skips the first argument and sets the second and third arguments to 0 and 1 respectively. The result is a function that returns the first character of a string.

Note: Usually your IDE should help but accessing the "…"-character (HORIZONTAL ELLIPSIS, U+2026) differs on various platforms.

  • Windows: ALT + 0133
  • Mac: ALT + ; or ALT + .
  • Linux: AltGr + .
use function React\Partial\bind;
use function React\Partial\;

$firstChar = bind('substr', (), 0, 1);
$mapped = array_map($firstChar, array('foo', 'bar', 'baz'));

var_dump($mapped);
// outputs ['f', 'b', 'b']

Tests

To run the test suite, you need PHPUnit.

$ phpunit

License

MIT, see LICENSE.

react/partial 适用场景与选型建议

react/partial 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 386.84k 次下载、GitHub Stars 达 114, 最近一次更新时间为 2013 年 08 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「functional-programming」 「partial」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 react/partial 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 386.84k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 115
  • 点击次数: 20
  • 依赖项目数: 12
  • 推荐数: 1

GitHub 信息

  • Stars: 114
  • Watchers: 14
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-08-30