daveross/functional-programming-utils
最新稳定版本:4.0.0
Composer 安装命令:
composer require daveross/functional-programming-utils
包简介
Functional Programming utilities for PHP 5.4+
README 文档
README
Functional Programming utilities for PHP 5.4+
Table of Contents
- Installation
- License
- Contributing
- Further reading & suggested viewing
- A note for PHP 5.6+ users
- Features
- Release History
Installation
Using composer
Put the require statement for functional-programming-utils in your composer.json file and run composer install or php composer.phar install:
{
"require": {
"daveross/functional-programming-utils": "~4.0"
}
}
Manually
Include all the files in the src directory, or individual files as needed:
<?php include 'path/to/functional-programming-utils/src/compose.php'; include 'path/to/functional-programming-utils/src/curry.php'; include 'path/to/functional-programming-utils/src/math.php'; include 'path/to/functional-programming-utils/src/memoize.php'; include 'path/to/functional-programming-utils/src/prop.php'; include 'path/to/functional-programming-utils/src/Monads/Monad.php'; include 'path/to/functional-programming-utils/src/Monads/Just.php'; include 'path/to/functional-programming-utils/src/Monads/Maybe.php'; include 'path/to/functional-programming-utils/src/Monads/Either.php'; include 'path/to/functional-programming-utils/src/Monads/Left.php'; include 'path/to/functional-programming-utils/src/Monads/Right.php';
License
See why I contribute to open source software.
Contributing
Pull requests are welcome. Unit tests are encouraged but not required.
Further reading & suggested viewing
- Professor Frisby's Mostly Adequate Guide to Functional Programming
- Functional Programming in PHP
- Learn You a Haskell for Great Good to see how a language built for Functional Programming works
Simon Holywell's Functional PHP talk at PHP Hampshire Feb 2014
A note for PHP 5.6+ users
Starting in PHP 5.6, you can use function at the top of a file to reference that function without typing its whole name, including the namespace. I encourage you to try it.
use function DaveRoss\FunctionalProgrammingUtils\add as add; $x = add( 5, 5 ); // 10
Features
Mathematical functions
add
Adds two values
$x = DaveRoss\FunctionalProgrammingUtils\add( 5, 5 ); // 10
subtract
Subtracts two values
$x = DaveRoss\FunctionalProgrammingUtils\subtract( 10, 5 ); // 5
multiply
Multiplies two numbers
$x = DaveRoss\FunctionalProgrammingUtils\multiply( 5, 5 ); // 25
divide
Divides two numbers
$x = DaveRoss\FunctionalProgrammingUtils\divide( 25, 5 ); // 5
modulus
Computes the remainder after division
$x = DaveRoss\FunctionalProgrammingUtils\modulus( 13, 5 ); // 3
inverse
Inverts a number
$x = DaveRoss\FunctionalProgrammingUtils\inverse( 5 ); // -5
truthy
Checks if a value evaluates to true using standard PHP rules
$x = DaveRoss\FunctionalProgrammingUtils\truthy( 5 ); // true $x = DaveRoss\FunctionalProgrammingUtils\truthy( 0 ); // false
true
Checks if a value is boolean true
$x = DaveRoss\FunctionalProgrammingUtils\true( true ); // true $x = DaveRoss\FunctionalProgrammingUtils\true( 5 ); // false
falsy
Checks if a value evaluates to false using standard PHP rules
$x = DaveRoss\FunctionalProgrammingUtils\falsy( 0 ); // true $x = DaveRoss\FunctionalProgrammingUtils\falsy( 5 ); // false
false
Checks if a value is boolean false
$x = DaveRoss\FunctionalProgrammingUtils\false( false ); // true $x = DaveRoss\FunctionalProgrammingUtils\false( 0 ); // false
default_value
Returns a value, or a default if the value is null
$x = DaveRoss\FunctionalProgrammingUtils\default_value( 5, 10); // 10 $x = DaveRoss\FunctionalProgrammingUtils\default_value( 5, null); // 5
Property Access
array_prop
Returns a value from an array given the corresponding key, or null if the key doesn't exist in the array
$a = array( 'hello' => 'world', 'a' => 'b' ); $x = DaveRoss\FunctionalProgrammingUtils\array_prop( $a, 'hello'); // 'world' $x = DaveRoss\FunctionalProgrammingUtils\array_prop( $a, 'test'); // null
object_prop
Returns a value from an object given the corresponding property name, or null if the property doesn't exist in the object
$o = new stdClass(); $o->hello = 'world'; $o->a = 'b'; $x = DaveRoss\FunctionalProgrammingUtils\object_prop( $o, 'hello'); // 'world' $x = DaveRoss\FunctionalProgrammingUtils\object_prop( $o, 'test'); // null
prop
Calls array_prop or object_prop as appropriate
$a = array( 'hello' => 'world', 'a' => 'b' ); $x = DaveRoss\FunctionalProgrammingUtils\prop( $a, 'hello'); // 'world' $x = DaveRoss\FunctionalProgrammingUtils\prop( $a, 'test'); // null $o = new stdClass(); $o->hello = 'world'; $o->a = 'b'; $x = DaveRoss\FunctionalProgrammingUtils\prop( $o, 'hello'); // 'world' $x = DaveRoss\FunctionalProgrammingUtils\prop( $o, 'test'); // null
Memoization
memoize
Wraps a function in a layer that stores the function's return value for every set of parameters it's called with, so the function doesn't need to be called again the next time it's called with the same parameters
$f = DaveRoss\FunctionalProgrammingUtils\memoize(function($a) { return $a; }); $x = $f(5); // 5 $x = $f(5); // 5 again, but the function didn't need to be called a second time
Currying
See Curry or Partial Application? The Difference Between Partial Application and Curry for details on how these functions differ.
partially_apply
Partially applies a function. Given a function that takes more than one parameter, returns a function that already knows the first parameter.
$add_five = DaveRoss\FunctionalProgrammingUtils\partially_apply( 'DaveRoss\FunctionalProgrammingUtils\add', 5 ); $x = $add_five( 5 ); // 10
partially_apply_right
Partially applies a function. Given a function that takes more than one parameter, returns a function that already knows the last parameter.
$divide_by_five = DaveRoss\FunctionalProgrammingUtils\partially_apply_right( 'DaveRoss\FunctionalProgrammingUtils\divide', 5 ); $x = $divide_by_five( 25 ); // 5
curry
Curries a function. Given a function that takes more than one parameter, applies a single parameter to it and returns a function that takes the next parameter until all required parameters are provided.
function add_three_integers($a, $b, $c) { return intval( $a ) + intval( $b ) + intval( $c ); } $fn = DaveRoss\FunctionalProgrammingUtils\curry( 'add_three_integers' , 1 ); $fn2 = $fn( 2 ); $x = $fn2( 3 ); // 6
Composition
compose
Creates a new function consisting of a series of functions that each take one parameter. When the new function is called, that series of functions is called from right to left, processing the result of the previous function.
$backwards_and_uppercase = DaveRoss\FunctionalProgrammingUtils\compose( 'str_reverse', 'strtoupper' ); $x = $backwards_and_uppercase( 'dlrow olleh' ); // HELLO WORLD
Monads
Monad
Abstract parent class for Monads. A Monad is a class that wraps a single value and implements function map(callable $f).
Monad::map() returns another Monad wrapping the function's return value. See the Just Monad.
Just
The Just Monad "just" wraps a value and maps functions to it.
$x = new Just( 5 ); $y = $x->map( function( $a ) { return $a * 5; } ); // Just(25)
Maybe
The Maybe Monad recognizes when it's holding a null value and returns Maybe( null ) when a function is mapped to it. Otherwise, it behaves like a Just Monad.
$x = new Maybe( 5 ); $y = $x->map( function( $a ) { return $a * 5; } ); // Maybe(25) $a = new Maybe( null ); $b = $a->map( function( $a ) { return $a * 5; } ); // Maybe(null)
maybe function
May be used to extract the value from a Maybe Monad.
$x = new Maybe( 5 ); $y = maybe(null, function( $a ) { return $a * 5; }, $x); // 25
Either
To implement conditionals, a function can be defined as returning either one value or another. This is represented with the Either Monad and its children, Left and Right.
Left
The Left Monad wraps an error value from an unsuccessful function call.
$f = function($a) { return ( $a < 10 ) : Left::of( 'too low' ) : Right::of( $a + 1 ); } $x = $f( 15 ); // Right( 16 ) $y = $f( 5 ); // Left( "too low" )
Right
The Right Monad wraps the result of a successful function call.
$f = function($a) { return ( $a < 10 ) : Left::of( 'too low' ) : Right::of( $a + 1 ); } $x = $f( 15 ); // Right( 16 ) $y = $f( 5 ); // Left( "too low" )
either function
May be used to extract the value from either a Left Monad or a Right Monad.
$x = Left::of( 5 ); $y = Right::of( 7 ); $left_handler = function( $a ) { return $a * 2; }; $right_handler = function( $a ) { return $a * 3; }; $a = either($left_handler, $right_handler, $x); // 10 $b = either($left_handler, $right_handler, $y); // 21
Release History
daveross/functional-programming-utils 适用场景与选型建议
daveross/functional-programming-utils 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.24k 次下载、GitHub Stars 达 21, 最近一次更新时间为 2015 年 08 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「curry」 「memoize」 「functional programming」 「functor」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 daveross/functional-programming-utils 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 daveross/functional-programming-utils 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 daveross/functional-programming-utils 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Non-standard PHP library (NSPL) - functional primitives toolbox and more
Connection 'Db' codeception module to 'Yii2' module database settings
Auto-curried function library
Functional library for php with proper currying
Iteration tools for PHP
A small helper for memoizing.
统计信息
- 总下载量: 5.24k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 22
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-08-18