深入探索PHP函数式编程的高级技巧
1. 函数式编程基础回顾
在函数式编程里,函数的结构化方式有多种,像递归和部分函数等技巧能让函数更具灵活性。例如下面这个封装 str_repeat 的函数:
# Our function to wrap str_repeat.
# Note it takes one parameter, the $count
function repeat_str($count) {
# This function returns another (closure) function,
# which binds $count, and accepts a single parameter
# $string. Note that *this* returned closure is the
# actual function that gets used in compose().
return function ($string) use ($count) {
return str_repeat($string, $count);
};
};
# Now let's compose those two functions together.
$ten_chars = compose(
repeat_str(10),
'display'
);
# and run our composed function
echo $ten_chars('*');
执行这段代码会输出 **********
超级会员免费看
订阅专栏 解锁全文
54

被折叠的 条评论
为什么被折叠?



