承接 improved/function 相关项目开发

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

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

improved/function

最新稳定版本:v1.0.0

Composer 安装命令:

composer require improved/function

包简介

Function handling and functional programming

README 文档

README

improved PHP library

function handling

PHP Scrutinizer Code Quality Code Coverage Packagist Stable Version Packagist License

Library for function handling and functional programming.

Installation

composer require improved/function

Functions

function_pipe

callable function_pipe(callable ...$functions)

Combine all functions, piping the output from one function to the input of the other.

Each callable should only require one argument, use short closures fn() if needed.

use Improved as i;

$slugify = i\function_pipe(
    fn($str) => i\string_case_convert($str, i\STRING_LOWERCASE),
    'Improved/string_remove_accents',
    'trim',
    fn($str) => preg_replace('/\W+/', '-', $str)
);

$slugify("Bonjour du monde / Français "); // "bonjour-du-monde-francais" 

function_all

callable function_all(callable ...$functions)

Call all functions sequentially. The arguments are passed to each function. The first argument is typically an accumulator.

Functions are expected to not return anything (void). If anything is returned, it's ignored.

use Improved as i;

$make = i\function_all(
    static function(ArrayObject $acc, array $opts): void {
        if (in_array('skip-prepare', $opts, true)) return;
        $acc[] = 'prepare';
    },
    new Compiler(), // Invokable object
    static function(ArrayObject $acc, array $opts): void {
        $acc[] = 'finish';
    }
);

$acc = new ArrayObject();
$opts = [/* ... */];

$make($acc, $opts);

function_trampoline

callable function_trampoline(callable $callable)

Return a new function that decorates given function with tail recursion optimization.

In traditional recursion, the typical model is that you perform your recursive calls first, and then you take the return value of the recursive call and calculate the result. In this manner, you don't get the result of your calculation until you have returned from every recursive call.

The problem with traditional recursion is that it builds up a call stack, limiting the amount of recusion you should allow.

Warning: Uncaught Error: Maximum function nesting level of '256' reached, aborting!

In tail recursion, you perform your calculations first, and then you execute the recursive call, passing the results of your current step to the next recursive step. This results in the last statement being in the form of return recursive_function(params, accumulator).

The result is calculated via the accumulator, so the return value of any given recursive step is the same as the return value of the next recursive call.

$sum_of_range = i\function_trampoline(function ($from, $to, $acc = 0) use (&$sum_of_range) {
    if ($from > $to) {
        return $acc;
    }
    
    return $sum_of_range($from + 1, $to, $acc + $from);
});

$sum_of_range(1, 10000); // 50005000;

Tail recursion optimization can automatically detect such a pattern and apply this as a consecutive call rather than nesting. Unfortunately this isn't implemented by PHP, so using a trampoline function is required.

Use function_trampoline in case of deep recursion (10+ levels).

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-10-06