PHP闭包Closure与array_reduce结合的一个范例
最近在研究laravel5.5的源代码,发现了其中的一段代码觉得挺有意思!
文件:vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php
public function then(Closure $destination)
{
$pipeline = array_reduce(
array_reverse($this->pipes), $this->carry(), $this->prepareDestination($destination)
);
return $pipeline($this->passable);
}
protected function carry()
{
return function ($stack, $pipe) {
return function ($passable) use ($stack, $pipe) {
if (is_callable($pipe)) {
// If the pipe is an instance of a Closure, we will just call it directly but
// otherwise we'll resolve the pipes out of the container and call it with
// the appropriate method and arguments, returning the results back out.
return $pipe($passable, $stack);
} elseif (! is_object($pipe)) {
list($name, $parameters) = $this->parsePipeString($pipe); // If the pipe is a string we will parse the string and resolve the class out
// of the dependency injection container. We can then build a callable and
// execute the pipe function giving in the parameters that are required.
$pipe = $this->getContainer()->make($name); $parameters = array_merge([$passable, $stack], $parameters);
} else {
// If the pipe is already an object we'll just make a callable and pass it to
// the pipe as-is. There is no need to do any extra parsing and formatting
// since the object we're given was already a fully instantiated object.
$parameters = [$passable, $stack];
}
return method_exists($pipe, $this->method)
? $pipe->{$this->method}(...$parameters)
: $pipe(...$parameters);
};
};
}
此段代码初看上去让人很迷惑,特作以下demo分解:
//闭包与array_reduce结合测试
$a = array( 1 , 2 , 3 , 4 , 5 );
$b = array_reduce ( $a , function($v , $w){
return function($p) use($v, $w){
var_dump($v);
};
} ); if(is_callable($b)){
$b('spring');
}
输出:
object(Closure)#5 (2) {
["static"]=>
array(2) {
["v"]=>
object(Closure)#4 (2) {
["static"]=>
array(2) {
["v"]=>
object(Closure)#3 (2) {
["static"]=>
array(2) {
["v"]=>
object(Closure)#2 (2) {
["static"]=>
array(2) {
["v"]=>
NULL
["w"]=>
int(1)
}
["parameter"]=>
array(1) {
["$p"]=>
string(10) "<required>"
}
}
["w"]=>
int(2)
}
["parameter"]=>
array(1) {
["$p"]=>
string(10) "<required>"
}
}
["w"]=>
int(3)
}
["parameter"]=>
array(1) {
["$p"]=>
string(10) "<required>"
}
}
["w"]=>
int(4)
}
["parameter"]=>
array(1) {
["$p"]=>
string(10) "<required>"
}
}
PHP闭包Closure与array_reduce结合的一个范例的更多相关文章
- JavaScript闭包(Closure)
JavaScript闭包(Closure) 本文收集了多本书里对JavaScript闭包(Closure)的解释,或许会对理解闭包有一定帮助. <你不知道的JavsScript> Java ...
- js中有趣的闭包(closure)
一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域无非就是两种:全局变量和局部变量. Javascript语言的特殊之处,就在于函数内部可以直接读取全局变量 ...
- 聊一下JS中的作用域scope和闭包closure
聊一下JS中的作用域scope和闭包closure scope和closure是javascript中两个非常关键的概念,前者JS用多了还比较好理解,closure就不一样了.我就被这个概念困扰了很久 ...
- python 函数对象(函数式编程 lambda、map、filter、reduce)、闭包(closure)
1.函数对象 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 秉承着一切皆对象的理念,我们再次回头来看函数(function).函 ...
- 深入理解JavaScript闭包(closure)
最近在网上查阅了不少javascript闭包(closure)相关的资料,写的大多是非常的学术和专业.对于初学者来说别说理解闭包了,就连文字叙述都很难看懂.撰写此文的目的就是用最通俗的文字揭开Java ...
- [转] Java内部类之闭包(closure)与回调(callback)
闭包(closure)是一个可调用的对象,它记录了一些信息,这些信息来自于创建它的作用域.通过这个定义,可以看出内部类是面向对象的闭包,因为它 不仅包含外围类对象(创建内部类的作用域)的信息,还自动拥 ...
- JavaScript 进阶(四)解密闭包closure
闭包(closure)是什么东西 我面试前端基本都会问一个问题"请描述一下闭包".相当多的应聘者的反应都是断断续续的词,“子函数”“父函数”“变量”,支支吾吾的说不清楚.我提示说如 ...
- javascript中的闭包(Closure)的学习
闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 下面是我在网上通过学习阮一峰老师的笔记,感觉总结很不错,特记录于此. 一.变量的作用域 要理解 ...
- [转载]学习Javascript闭包(Closure)
学习Javascript闭包(Closure) 源地址: http://www.ruanyifeng.com/blog/2009/08/learning_javascript_closures ...
随机推荐
- 例子:web版坦克大战1.0
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- VS的使用插件
1. 插件安装: 1) productivity power tools:代码查看优化插件: 2) Visaul Studio Color Theme Editor 主题修改插件: 3) VS ...
- 矩阵[雅礼集训 2017 Day1]
SOL 奇奇怪怪的贪心(你也不要问我为什么) #include<bits/stdc++.h> #define N 1007 int f[N][N],sum,ans,Ha,bo,n,Ans; ...
- Django学习日记06_视图_URLconf、View
URLconf Django通过URLconf来响应网页请求,在项目设置settings.py中,设定了ROOT_URLCONF值来指定默认的URLconf(即mysite.urls),当HTTPRe ...
- 物流包裹一站式查询(TrackingMore)
快递查询接口 目前提供快递查询的接口平台有: Trackingmore 快递100 快递网 不同接口的区别: (1)Trackingmore支持380家快递公司,其中有55家为国内的快递,其余325家 ...
- js介绍
---恢复内容开始--- js介绍 最近学习了js,那么我今天给大家介绍下js.希望对初学者有些帮助. js就是常说的JavaScript.JavaScript是目前世界上最流行的编程语言之一.这门语 ...
- C# log4net 的配置
项目的日志组件是必备可少的,任何项目中都需要.这样既方便前期的开发测试也方便项目后期的项目维护.C#项目的一个不错的日志组件是log4net,下面我就把桌面应用程序.控制台程序.网站中log4net的 ...
- [Spark内核] 第36课:TaskScheduler内幕天机解密:Spark shell案例运行日志详解、TaskScheduler和SchedulerBackend、FIFO与FAIR、Task运行时本地性算法详解等
本課主題 通过 Spark-shell 窥探程序运行时的状况 TaskScheduler 与 SchedulerBackend 之间的关系 FIFO 与 FAIR 两种调度模式彻底解密 Task 数据 ...
- arm swi 软中断 一例
原文在CU,挪过来了. 1. 目标 本文单纯验证swi指令相关功能 2. 环境 vmware + redhat 9 + arm-elf-gcc 2.95 + skyeye-1.2.6_rc1(模拟s3 ...
- unisound_asr 云知声 语音识别 python版接口
抽空,实现了一份云知声 语音转写的python版本. 使用python通过调用动态库实现. 云知声官网: http://dev.hivoice.cn/sdk_download/schema_sdk.j ...