functional-php/pattern-matching
Composer 安装命令:
composer require functional-php/pattern-matching
包简介
Pattern matching for PHP with automatic destructuring.
README 文档
README
Pattern matching is the process of checking a series of token against a pattern.
It is different from pattern recognition as the match needs to be exact.
The process does not only match as a switch statement does, it also assigns the value
a bit like the list construct in PHP, a process called destructuring.
Most functional languages implement it as a core feature. Here is are some small examples in Haskell:
fact :: (Integral a) => a -> a fact 0 = 1 fact n = n * fact (n-1) head :: [a] -> a head xs = case xs of [] -> error "empty list" (x:_) -> x firstThree :: [a] -> (a, a, a) firstThree (x:y:z:_) = (x, y, z) firstThree _ = error "need at least 3 elements"
If you want to read more about the topic, you can head over to Wikipedia : Pattern matching
Installation
composer require functional-php/pattern-matching
Basic Usage
As we cannot extend the syntax of PHP, the choice was made to use a syntax based on arrays. The key representes the pattern and the value is the function to call with the value or a constant if you want to do nothing with it.
Let's see how we could implement our 3 Haskell examples:
use FunctionalPHP\PatternMatching as m; $fact = m\func([ '0' => 1, 'n' => function($n) use(&$fact) { return $n * $fact($n - 1); } ]); $head = m\func([ '(x:_)' => function($x) { return $x; }, '_' => function() { throw new RuntimeException('empty list'); } ]); $firstThree= m\func([ '(x:y:z:_)' => function($x, $y, $z) { return [$x, $y, $z]; }, '_' => function() { throw new RuntimeException('need at least 3 elements'); } ]);
You can also use the match function if you want to have a beefed up version of the switch statement or if you don't like anonymous functions:
use FunctionalPHP\PatternMatching as m; function factorial($n) { return m\pmatch([ '0' => 1, 'n' => function($n) use(&$fact) { return $n * factorial($n - 1); } ], $n); } echo m\pmatch([ '"toto"' => 'first', '[a, [b, c], d]' => 'second', '[a, _, (x:xs), c]' => 'third', '_' => 'default', ], [1, 2, ['a', 'b'], true]); // third
If you are just interested in destructuring your values, there is also a helper for that:
use FunctionalPHP\PatternMatching as m; print_r(m\extract([1, 2, ['a', 'b'], true], '[a, _, (x:xs), c]')); // Array ( // [a] => 1 // [x] => a // [xs] => Array ( [0] => b ) // [c] => 1 // )
Patterns
Here is a quick recap of the available patterns:
| Name | Format | Example |
|---|---|---|
| Constant | Any scalar value (int, float, string, boolean) | 1.0, 42, "test" |
| Variable | identifier |
a, name, anything |
| Array | [<pattern>, ..., <pattern>] |
[], [a], [a, b, c] |
| Cons | (identifier:list-identifier) |
(x:xs), (x:y:z:xs) |
| Wildcard | _ |
_ |
| As | identifier@(<pattern>) |
all@(x:xs) |
Testing
You can run the test suite for the library using:
composer test
A test report will be available in the reports directory.
Contributing
Any contribution welcome :
- Ideas
- Pull requests
- Issues
functional-php/pattern-matching 适用场景与选型建议
functional-php/pattern-matching 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 83.39k 次下载、GitHub Stars 达 82, 最近一次更新时间为 2016 年 10 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「pattern」 「functional」 「Pattern Matching」 「destructuring」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 functional-php/pattern-matching 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 functional-php/pattern-matching 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 functional-php/pattern-matching 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Connection 'Db' codeception module to 'Yii2' module database settings
PHP Interface for Babel Street Text Analytics
Functional library for php with proper currying
Iteration tools for PHP
A Laravel package for the Repository Design Pattern.
A PHP library with partial functions suitable for the pipe operator
统计信息
- 总下载量: 83.39k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 83
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2016-10-22