wojciech.nawalaniec/phlambda 问题修复 & 功能扩展

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

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

wojciech.nawalaniec/phlambda

Composer 安装命令:

composer require wojciech.nawalaniec/phlambda

包简介

Functional library for PHP simillar to JS Rambda (https://github.com/ramda/ramda).

README 文档

README

Build Status Version Coverage Status Mutation testing badge Latest Stable Version Total Downloads License PHP Version Require

Small library for PHP with set of usefull functions for functional programming.

Features:

  • set of useful functions helpful in functional programming
  • all functions are automatically currying
  • every array can be wrapped with special class which allows for method chaining
  • first param is always function and the data to be operated will be last param

Table of contents

Installation

To install run:

composer require wojciech.nawalaniec/phlambda

Usage

All functions and constants are in one namespace, so it won't create any conflict with existing ones in your project or other libraries. You can simply import namespaces with alias (for simplification), like this:

use Wojciech\Phlambda as f;

f\all(f\above(2), [1, 2, 3, 4]);

You can import one or few functions using import like this:

use function Wojciech\Phlambda\{all, above};

all(above(2), [1, 2, 3, 4]);

There are also constants which can be used in places accepting callbacks, because PHP will resolve them as functions:

use function Wojciech\Phlambda\map;
use const Wojciech\Phlambda\toString;

map(toString, [1, 2, 3]);

If you have some array and wish to perform multiple, chained operations on it. You can wrapp it with special object with _() function:

use function Wojciech\Phlambda\{_, below, concat};
use const Wojciech\Phlambda\toString;

_($someArray)
    ->filter(below(30))
    ->map(toString) // you can use constant which will be resolved as callable by PHP
    ->reduce(concat(), ''); // or you can call function without params because all functions all curried

If you wish to have more readable code you can use a static method from that class instead: Wrapper::wrap($array).

If you don't want to use objects you can use just functions. Wrapper's methods are just delegates to those functions, and exists only for chaining purposes.

Docs

Here you can find documentation.

Currying

What makes that library different from other functional libraries for PHP is that all function are automatically curried. If you don't know what currying functions are let me try to change it. According to Wikipedia:

In mathematics and computer science, currying is the technique of converting a function that takes multiple arguments into a sequence of functions that each takes a single argument.

Let's see an example:

// we can use reduce function normally like this:
$array = ['a', 'b', 'c'];
$result = reduce(\Wojciech\Phlambda\concat, '', $array); // $result = 'abc'

// and we can use it like currying function:
$concat = reduce(\Wojciech\Phlambda\concat, ''); // now it will return callback accepting last param from reduce - an array
$result = $concat($array); // $result = 'abc'

Because I've decided to implement currying, I've also added placeholder feature. A placeholder is special type you can pass to currying function, if you want to postpone passing of some arguments.

Example of placeholder usage:

$array = [1, 2, 3];
$reducer = reduce(__, __, $array);

$sum = $reducer(sum, 0);
$multiplication = $reducer(multiply, 0);

Development Guides

Check out CONTRIBUTING file

Tools

  • make build-docs - generates HTML documentation. Requires Docker installed
  • make generate-constants - generates src/constants.php file, for all functions

Backstory

PHP was not designed as functional programming language, that's one thing I'm sure. We can create and use anonymous functions, and in 7.4 we even got arrow functions fn () =>. Language itself is providing us with some functions like array_map() etc. where we can pass an array and some callable to perform some operations on the input. But its... so cumbersome.

One day at the company my friend joked about PHP and said that it's sad we can't do something like this in PHP:

someArray.every(below(30));

I thought for a while, and respond with something like this:

function below(int $val): callable
{
    return fn(int $v) => $v < $val;
}

function every(array $a, callable $fn): array
{
    return array_filter($a, $fn);
}

$arr = [1, 2, 3, 4];
every($arr, below(3));

Both examples are similar, but we can see that with PHP solution we have no way of chaining methods together, also we must write most of that method our selves. After while another idea came up to my mind. We could leverage OOP and create class which implements \ArrayAccess so it can be considered array like, and have all that nice methods we can chain together. I thought about something like this:

$arrayObject
    ->filter(below(30))
    ->sum();

It seemed as a great weekend project so here it is.

There are some great functional libraries for JS, but one (pointed by earlier mentioned friend) seemed interesting. It's Ramda. It's interesting because it has all function automatically curried, and it adds some complexity to that project.

wojciech.nawalaniec/phlambda 适用场景与选型建议

wojciech.nawalaniec/phlambda 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17 次下载、GitHub Stars 达 5, 最近一次更新时间为 2022 年 01 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 wojciech.nawalaniec/phlambda 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 5
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-01-06