如何读懂并写出装逼的函数式代码?

2018年6月5日08:58:06 3 739 阅读

对于函数式的版本,乍一看,的确令人非常费解,仔细看一下,你可能就晕掉了,似乎完全就是天书,看上去非常装逼,哈哈。不过,我感觉解析那段函数式的代码可能会一个比较有趣过程。

先看代码

这个代码平淡无奇,就是从一个数组中找到一个数,O(n)的算法,找不到就返回 null。

 

下面是正常的 old-school 的方式。不用多说。


1
2
3
4
5
6
7
<span class="hljs-default-comment" style="color: #5c6370; background: rgba(0, 0, 0, 0); display: inline; width: 74px; text-decoration: none solid #5c6370; font-weight: 400; font-style: italic;">//正常的版本</span>
<span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 151px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 58px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">function</span> <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">find</span> (<span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">x, y</span>) </span>{
  <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 22px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">for</span> ( <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 21px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">let</span> i = <span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 8px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">0</span>; i &lt; x.length; i++ ) {
    <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 14px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">if</span> ( x[i] == y ) <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 44px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">return</span> i;
  }
  <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 44px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">return</span> <span class="hljs-default-literal" style="color: #56b6c2; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #56b6c2; font-weight: 400; font-style: normal;">null</span>;
}

let arr = [0,1,2,3,4,5]
console.log(find(arr, 2))
console.log(find(arr, 8))


1
 

结果到了函数式成了下面这个样子(好像上面的那些代码在下面若影若现,不过又有点不太一样,为了消掉if语言,让其看上去更像一个表达式,动用了 ? 号表达式):


1
2
3
4
5
6
7
<span class="hljs-default-comment" style="color: #5c6370; background: rgba(0, 0, 0, 0); display: inline; width: 86px; text-decoration: none solid #5c6370; font-weight: 400; font-style: italic;">//函数式的版本</span>
<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 36px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">const</span> find = ( <span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 7px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">f</span> =&gt;</span> f(f) ) ( <span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 7px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">f</span> =&gt;</span>
  (<span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 51px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">next</span> =&gt;</span> (x, y, i = <span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 7px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">0</span>) =&gt;
    ( i &gt;= x.length) ?  <span class="hljs-default-literal" style="color: #56b6c2; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #56b6c2; font-weight: 400; font-style: normal;">null</span> :
      ( x[i] == y ) ? i :
        next(x, y, i+<span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 7px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">1</span>))(<span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 87px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(<span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 50px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">...args</span>) =&gt;</span>
          (f(f))(...args)))

let arr = [0,1,2,3,4,5]
console.log(find(arr, 2))
console.log(find(arr, 8))


1
 

为了讲清这个代码,需要先补充一些知识。

Javascript的箭头函数

首先先简单说明一下,ECMAScript2015 引入的箭头表达式。箭头函数其实都是匿名函数,其基本语法如下:


1
2
3
(param1, param2, …, paramN) =&gt; { statements }
(param1, param2, …, paramN) =&gt; expression
     <span class="hljs-default-comment" style="color: #5c6370; background: rgba(0, 0, 0, 0); display: inline; width: 262px; text-decoration: none solid #5c6370; font-weight: 400; font-style: italic;">// 等于 :  =&gt; { return expression; } </span>

// 只有一个参数时,括号才可以不加:
(singleParam) => { statements }
singleParam => { statements }


1
 

//如果没有参数,就一定要加括号:
() => { statements }


1
 

下面是一些示例:


1
2
3
<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 21px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">var</span> simple = <span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 7px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">a</span> =&gt;</span> a &gt; <span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 14px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">15</span> ? <span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 14px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">15</span> : a;
simple(<span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 15px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">16</span>); <span class="hljs-default-comment" style="color: #5c6370; background: rgba(0, 0, 0, 0); display: inline; width: 37px; text-decoration: none solid #5c6370; font-weight: 400; font-style: italic;">// 15</span>
simple(<span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 15px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">10</span>); <span class="hljs-default-comment" style="color: #5c6370; background: rgba(0, 0, 0, 0); display: inline; width: 37px; text-decoration: none solid #5c6370; font-weight: 400; font-style: italic;">// 10</span>

let max = (a, b) => a > b ? a : b;


1
 

// Easy array filtering, mapping, ...


1
 

var arr = [5, 6, 13, 0, 1, 18, 23];
var sum = arr.reduce((a, b) => a + b);  // 66
var even = arr.filter(v => v % 2 == 0); // [6, 0, 18]
var double = arr.map(v => v * 2);       // [10, 12, 26, 0, 2, 36, 46]


1
 

看上去不复杂吧。不过,上面前两个 simple 和 max 的例子都把这箭头函数赋值给了一个变量,于是它就有了一个名字。有时候,某些函数在声明的时候就是调用的时候,尤其是函数式编程中,一个函数还对外返回函数的时候。比如下在这个例子:


1
2
3
4
5
<span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 202px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 58px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">function</span> <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 79px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">MakePowerFn</span>(<span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 36px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">power</span>) </span>{
  <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 44px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">return</span> <span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 166px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 58px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">function</span> <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 50px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">PowerFn</span>(<span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 28px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">base</span>) </span>{
    <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 43px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">return</span> <span class="hljs-default-built_in" style="color: #e6c07b; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #e6c07b; font-weight: 400; font-style: normal;">Math</span>.pow(base, power);
  }
}

power3 = MakePowerFn(3); //制造一个X的3次方的函数
power2 = MakePowerFn(2); //制造一个X的2次方的函数


1
 

console.log(power3(10)); //10的3次方 = 1000
console.log(power2(10)); //10的2次方 = 100


1
 

其实,在 MakePowerFn 函数里的那个 PowerFn 根本不需要命名,完全可以写成:


1
2
3
4
5
<span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 202px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 58px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">function</span> <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 79px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">MakePowerFn</span>(<span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 36px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">power</span>) </span>{
  <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 44px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">return</span> <span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 108px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 58px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">function</span>(<span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">base</span>) </span>{
    <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 43px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">return</span> <span class="hljs-default-built_in" style="color: #e6c07b; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #e6c07b; font-weight: 400; font-style: normal;">Math</span>.pow(base, power);
  }
}

如果用箭头函数,可以写成:


1
2
3
4
5
MakePowerFn = <span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 65px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 36px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">power</span>  =&gt;</span> {
  <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 44px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">return</span> <span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 50px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">base</span> =&gt;</span> {
    <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 43px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">return</span> <span class="hljs-default-built_in" style="color: #e6c07b; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #e6c07b; font-weight: 400; font-style: normal;">Math</span>.pow(base, power);
  }
}

我们还可以写得更简洁(如果用表达式的话,就不需要 { 和 }, 以及 return 语句 ):


1
MakePowerFn = <span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 58px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 36px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">power</span> =&gt;</span> base =&gt; <span class="hljs-default-built_in" style="color: #e6c07b; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #e6c07b; font-weight: 400; font-style: normal;">Math</span>.pow(base, power)

我还是加上括号,和换行可能会更清楚一些:


1
2
3
<span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 173px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 79px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">MakePowerFn</span> = <span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 51px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(power)</span> =&gt;</span> (
  <span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 65px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 44px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(base)</span> =&gt;</span> (Math.pow(base, power))
)

好了,有了上面的知识,我们就可以进入一个更高级的话题——匿名函数的递归。

 

 

匿名函数的递归

函数式编程立志于用函数表达式消除有状态的函数,以及for/while循环,所以,在函数式编程的世界里是不应该用for/while循环的,而要改用递归(递归的性能很差,所以,一般是用尾递归来做优化,也就是把函数的计算的状态当成参数一层一层的往下传递,这样语言的编译器或解释器就不需要用函数栈来帮你保存函数的内部变量的状态了)。

 

好了,那么,匿名函数的递归该怎么做?

 

一般来说,递归的代码就是函数自己调用自己,比如我们求阶乘的代码:


1
2
3
4
<span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 115px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 58px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">function</span> <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">fact</span><span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 21px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(n)</span></span>{
  <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 44px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">return</span> n==<span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 8px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">0</span> ? <span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 8px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">1</span> :  n * fact(n<span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 14px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">-1</span>);
};
result = fact(<span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 7px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">5</span>);

在匿名函数下,这个递归该怎么写呢?对于匿名函数来说,我们可以把匿名函数当成一个参数传给另外一个函数,因为函数的参数有名字,所以就可以调用自己了。 如下所示:


1
2
3
function combinator(<span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 36px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>)</span> {
  <span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 94px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span><span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 43px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>)</span>;
}</span>

这个是不是有点作弊的嫌疑?Anyway,我们再往下,把上面这个函数整成箭头函数式的匿名函数的样子。


1
(<span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 151px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>) =&gt; <span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 79px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>(<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>)</span>)</span>

现在你似乎就不像作弊了吧。把上面那个求阶乘的函数套进来是这个样子:

首先,先重构一下fact,把fact中自己调用自己的名字去掉:


1
2
3
function fact(<span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 58px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>, <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 7px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">n</span>)</span> {
  <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 44px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">return</span> n==<span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 8px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">0</span> ? <span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 8px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">1</span> :  n * <span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 297px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span><span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 80px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>, n-1)</span>;
}</span>

fact(fact, 5); //输出120


1
 

然后,我们再把上面这个版本变成箭头函数的匿名函数版:


1
2
<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 21px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">var</span> fact = (<span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 427px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>, <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 7px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">n</span>) =&gt; <span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 239px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">( n==0 ? 1 :  n * <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>(<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>, n-1)</span> )
<span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">fact</span><span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 65px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(fact, 5)</span></span>

这里,我们依然还要用一个fact来保存这个匿名函数,我们继续,我们要让匿名函数声明的时候,就自己调用自己。

 

也就是说,我们要把 


1
(<span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 339px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>, <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 8px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">n</span>) =&gt; <span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 238px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">( n==0 ? 1 :  n * <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>(<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>, n-1)</span> )</span>

这个函数当成调用参数,传给下面这个函数:


1
(<span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 181px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>, <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 8px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">x</span>) =&gt; <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">func</span><span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 65px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>, x)</span></span>

最终我们得到下面的代码:


1
2
3
4
( (<span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 475px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>, <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 7px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">x</span>) =&gt; <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">func</span><span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 65px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>, x)</span> ) <span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 296px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(  //函数体
  (<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>, n)</span> =&gt; <span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 239px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">( n==0 ? 1 :  n * <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>(<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>, n-1)</span> ), //第一个调用参数
  5 //第二调用参数
);</span>

好像有点绕,anyway, 你看懂了吗?没事,我们继续。

 

动用高阶函数的递归

但是上面这个递归的匿名函数在自己调用自己,所以,代码中有hard code的实参。我们想实参去掉,如何去掉呢?我们可以参考前面说过的那个 MakePowerFn 的例子,不过这回是递归版的高阶函数了。


1
2
3
4
5
HighOrderFact = function(<span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 37px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>)</span>{
  <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 44px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">return</span> function(n){
    <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 43px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">return</span> n==<span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 7px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">0</span> ? <span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 7px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">1</span> : n * <span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 303px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 28px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span><span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 44px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>)</span><span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 36px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(n-1)</span>;
  };
};</span>

我们可以看,上面的代码简单说来就是,需要一个函数做参数,然后返回这个函数的递归版本。那么,我们怎么调用呢?


1
2
fact = HighOrderFact(HighOrderFact);
fact(5);

连起来写就是:


1
<span class="hljs-default-selector-tag" style="color: #e06c75; background: rgba(0, 0, 0, 0); display: inline; width: 94px; text-decoration: none solid #e06c75; font-weight: 400; font-style: normal;">HighOrderFact</span> ( HighOrderFact ) ( <span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 8px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">5</span> )

但是,这样让用户来调用很不爽,所以,以我们一个函数把 HighOrderFact ( HighOrderFact ) 给代理一下:


1
2
3
4
5
6
7
8
9
10
fact = <span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 145px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 58px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">function</span> <span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 73px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">( hifunc )</span> </span>{
  <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 44px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">return</span> hifunc ( hifunc );
} (
  <span class="hljs-default-comment" style="color: #5c6370; background: rgba(0, 0, 0, 0); display: inline; width: 123px; text-decoration: none solid #5c6370; font-weight: 400; font-style: italic;">//调用参数是一个函数</span>
  <span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 116px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 58px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">function</span> <span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 44px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(func)</span> </span>{
    <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 43px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">return</span> <span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 80px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 58px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">function</span><span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 22px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(n)</span></span>{
      <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 43px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">return</span> n==<span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 8px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">0</span> ? <span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 7px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">1</span> : n * func(func)(n<span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 14px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">-1</span>);
    };
  }
);

fact(5); //于是我们就可以直接使用了


1
 

用箭头函数重构一下,是不是简洁了一些?


1
2
3
fact = (highfunc =&gt; highfunc ( highfunc ) ) (
  <span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 332px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span> =&gt; <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 7px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">n</span> =&gt;  <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 8px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">n</span>==0 ? 1 : <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 7px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">n</span> * <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">func</span><span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 43px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>)</span><span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 36px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(n-1)</span>
);</span>

上面就是我们最终版的阶乘的函数式代码。

 

回顾之前的程序

我们再来看那个查找数组的正常程序:


1
2
3
4
5
6
7
<span class="hljs-default-comment" style="color: #5c6370; background: rgba(0, 0, 0, 0); display: inline; width: 74px; text-decoration: none solid #5c6370; font-weight: 400; font-style: italic;">//正常的版本</span>
<span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 151px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 58px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">function</span> <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">find</span> (<span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">x, y</span>) </span>{
  <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 22px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">for</span> ( <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 21px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">let</span> i = <span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 8px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">0</span>; i &lt; x.length; i++ ) {
    <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 14px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">if</span> ( x[i] == y ) <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 44px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">return</span> i;
  }
  <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 44px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">return</span> <span class="hljs-default-literal" style="color: #56b6c2; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #56b6c2; font-weight: 400; font-style: normal;">null</span>;
}

先把for干掉,搞成递归版本:


1
2
3
4
5
<span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 188px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 58px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">function</span> <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">find</span> <span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 79px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(x, y, i=<span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 7px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">0</span>)</span> </span>{
  <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 15px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">if</span> ( i &gt;= x.length ) <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 43px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">return</span> <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 28px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">null</span>;
  <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 15px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">if</span> ( x[i] == y ) <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 43px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">return</span> i;
  <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 44px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">return</span> find(x, y, i+<span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 7px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">1</span>);
}

然后,写出带实参的匿名函数的版本(注:其中的if代码被重构成了 ?号表达式):


1
2
3
4
5
6
7
8
( (<span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 383px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>, <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 7px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">x</span>, <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 8px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">y</span>, <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 7px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">i</span>) =&gt; <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">func</span><span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 109px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 28px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>, x, y, i)</span> ) <span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 383px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(  //函数体
  (<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>, x, y, i=0)</span> =&gt; <span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 354px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(
      i &gt;= x.length ?  null :
         x[i] == y  ?  i : <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span> (<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>, x, y, i+1)</span>
  ), //第一个调用参数
  <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 22px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">arr</span>, //第二调用参数
  2 //第三调用参数
)</span>

最后,引入高阶函数,去除实参:


1
2
3
4
5
6
<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 36px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">const</span> find = ( highfunc =&gt; highfunc( highfunc ) ) (
   <span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 376px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span> =&gt; <span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 94px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(x, y, i = 0)</span> =&gt; <span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 289px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(
     i &gt;= x.length ?  null :
           x[i] == y  ?  i : <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span> (<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 29px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">func</span>)</span> <span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 79px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(x, y, i+1)</span>
   )
);</span>

注:函数式编程装逼时一定要用const字符,这表示我写的函数里的状态是 immutable 的,天生骄傲!

 

再注:我写的这个比原来版的那个简单了很多,原来版本的那个又在函数中套了一套 next, 而且还动用了不定参数,当然,如果你想装逼装到天上的,理论上来说,你可以套N层,呵呵。

 

现在,你可以体会到,如此逼装的是怎么来的了吧?

 

其它

你还别说这就是装逼,简单来说,我们可以使用数学的方式来完成对复杂问题的描述,那怕是递归。其实,这并不是新鲜的东西,这是Alonzo Church 和 Haskell Curry 上世纪30年代提出来的东西,这个就是 Y Combinator 的玩法,关于这个东西,你可以看看下面两篇文章:

The Y Combinator (Slight Return)

https://mvanier.livejournal.com/2897.html

Wikipedia: Fixed-point combinator

https://en.wikipedia.org/wiki/Fixed-point_combinator

- The End -

 

本篇文章来源于微信公众号: 程序人生

张晔璇

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

目前评论:3   其中:访客  1   博主  2

    • avatar eee 0
      Firefox 60.0 Firefox 60.0 Mac OS X  10.13 Mac OS X 10.13
      Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:60.0) Gecko/20100101 Firefox/60.0

      es6哦

        • avatar 张晔璇 Admin
          Google Chrome 63.0.3239.26 Google Chrome 63.0.3239.26 Windows 7 x64 Edition Windows 7 x64 Edition
          Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36

          欢迎土豪朋友光临!

          @eee

          • avatar 张晔璇 Admin
            QQbrowser 10.1.1430.400 QQbrowser 10.1.1430.400 Windows 7 x64 Edition Windows 7 x64 Edition
            Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.5408.400 QQBrowser/10.1.1430.400

            你们都收不到我发的邮件吗?

            @eee