loophp/fpt

PHP 的函数式编程工具箱。

维护者

详细信息

github.com/loophp/fpt

源代码

问题

文档

赞助包维护!
drupol
Paypal

dev-master 2022-12-21 13:51 UTC

This package is auto-updated.

Last update: 2024-09-22 12:35:43 UTC


README

Latest Stable Version GitHub stars Total Downloads GitHub Workflow Status Scrutinizer code quality Type Coverage Code Coverage License Read the Docs Donate! Donate!

函数式编程工具箱

描述

Functional Programming Toolbox (FPT for the friends) 是一组无状态和不可变辅助类,旨在促进函数式编程 (FP) 概念的使用。

此项目并非旨在将 PHP 转变为一个功能完整的 FP 语言,但它将帮助用户在他们的代码中理解和使用 FP 概念的子集。

要求

  • PHP 8

安装

composer require loophp/fpt

使用

柯里化和部分应用

<?php

declare(strict_types=1);

namespace Example;

include __DIR__ . '/vendor/autoload.php';

use loophp\fpt\FPT;

// The "curry" method combine at the same time the
// Curry and Partial, see example below.
$explode = FPT::curry()('explode');

// Thanks to the new PHP 8 feature "named arguments",
// you can pass arguments in any order.

var_dump($explode(',', 'a,b,c'));
var_dump($explode(separator: ',', string: 'a,b,c'));
var_dump($explode(separator: ',')(string: 'a,b,c'));
var_dump($explode(string: 'a,b,c', separator: ','));
var_dump($explode(string: 'a,b,c')(separator: ','));

// All of these will return:
/**
 * array(3) {
 *   [0] => string(1) "a"
 *   [1] => string(1) "b"
 *   [2] => string(1) "c"
 * }
*/

// You can pass an optional "arity" argument

$explode = FPT::curry()('explode', 3);

var_dump($explode(',', 'a,b,c', 2));
var_dump($explode(separator: ',', string: 'a,b,c', limit: 2));
var_dump($explode(separator: ',')(string: 'a,b,c')(limit: 2));
var_dump($explode(limit: 2, string: 'a,b,c', separator: ','));
var_dump($explode(limit: 2)(string: 'a,b,c')(separator: ','));

// All of these will return:
/**
 * array(2) {
 *   [0] => string(1) "a"
 *   [1] => string(3) "b,c"
 * }
*/

组合

<?php

declare(strict_types=1);

namespace Example;

include __DIR__ . '/vendor/autoload.php';

use loophp\fpt\FPT;

$closure = static fn (string $first, string $second): string => sprintf("My cats names are %s and %s.", $first, $second);

$composedClosure = FPT::compose()('strtoupper', $closure);

$composedClosure('Izumi', 'Nakano'); // "MY CATS NAMES ARE IZUMI AND NAKANO."

文档

API 会给你一个现有方法和你可以用它做什么的良好印象。

我会尽最大努力保持文档更新,如果你发现了什么奇怪之处,请通过 问题队列 告诉我。

代码质量、测试和基准测试

每当向库中引入更改时,Github 会运行测试。

该库使用 PHPSpec 编写了测试。请随意在 spec 目录中查看它们。运行 composer phpspec 以触发测试。

在每次提交之前,会使用 GrumPHP 执行一些检查,运行 composer grumphp 以手动检查。

使用 Infection,一个 PHP 变异测试框架,测试测试的质量。运行 composer infection 以尝试。

静态分析器也控制代码。已启用 PHPStanPSalm 的最大级别。

贡献

请随时通过发送 Github pull requests 来贡献。我反应很快 :-)

如果你不能为代码做出贡献,你也可以在 GithubPaypal 上赞助我。

变更日志

请参阅 CHANGELOG.md 以查看基于 git 提交 的变更日志。

有关更详细的变更日志,请检查 发行变更日志

感谢

API 文档受到了 Ramda 项目的启发。