andres-ml/fpl 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

andres-ml/fpl

最新稳定版本:1.0.0

Composer 安装命令:

composer require andres-ml/fpl

包简介

Functional PHP library

README 文档

README

This library provides a series of curried, data-last functions commonly used within the functional programming paradigm.

use function Aml\Fpl\{compose, partial, last};

$lastWord = compose(last(...), partial(explode(...), ' '));
$lastWord('some words in a sentence'); // 'sentence'

The new PHP 8.5 pipe operator and the recent variadic placeholder make this library less necessary, but you still may find it useful if you dislike the boilerplate and the lack of partial application.

// idiomatic php
'some words in a BUG sentence'
    |> (fn($x) => explode(' ', $x))
    |> (fn($x) => array_filter($x, fn(string $word) => $word !== 'BUG'))
    |> (fn($x) => explode(' ', $x));
    // 'some words in a sentence'

// with this library
use function Aml\Fpl\{partial as _, filter};
'some words in a BUG sentence'
    |> _(explode(...), ' ')
    |> filter(fn(string $word) => $word !== 'BUG')
    |> _(implode(...), ' ');
    // 'some words in a sentence'

API

Full api documentation can be found in docs/api.md

Installation

composer require andres-ml/fpl

Non-curried functions

If you want, you can use the original function definitions instead. Note that these are not curried and don't have their corresponding const versions.

use function Aml\Fpl\functions\{map};

Arrays and iterators

List-related functions return an array when they receive an array parameter, but they return iterators when receiving iterators (and can thus behave lazily). You can force laziness over an initial array by previously using toIterator or finish off with an array by ending with toArray:

use Aml\Fpl;

$lowerThan4 = function($x) { return $x < 4; };

Fpl\takeWhile($lowerThan4, [0, 1, 2, 3, 4, 5]); // [0, 1, 2, 3]

$pickLowerThan4 = Fpl\compose(
    Fpl\toArray
    Fpl\takeWhile($lowerThan4),
    Fpl\counter
);

$pickLowerThan4(); // [0, 1, 2, 3]

Code generation

Functions are defined on src/api/*.php. They are split among different files but share the Aml\Fpl\functions namespace. All functions defined on those files not beginning with an underscore _ will be parsed and added to build/api.php as calls to their curried versions.

Code can be generated by running make code, which uses src/build.php.

Documentation is also automatically generated through make docs, which uses src/make-docs.php.

Testing

Testing is done through phpunit: ./vendor/bin/phpunit tests or make test

Note on performance

Automatic currying and automatic array/iter support have a performance overhead. Be wary of using them in a time-critical loop; a native array_map will always be faster than other implementations of map.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-03-29