CSDN程序人生公众号官方博客

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

https://coolshell.cn/articles/17524.html

//正常的版本function find (x, y) {  for ( let i = 0; i < x.length; i++ ) {    if ( x[i] == y ) return i;  }  return null;}let arr = [0,1,2,3,4,5]console.log(find(arr, 2))console.log(find(arr, 8))

//函数式的版本const find = ( f => f(f) ) ( f =>  (next => (x, y, i = 0) =>    ( i >= x.length) ?  null :      ( x[i] == y ) ? i :        next(x, y, i+1))((...args) =>          (f(f))(...args)))let arr = [0,1,2,3,4,5]console.log(find(arr, 2))console.log(find(arr, 8))

Javascript的箭头函数

(param1, param2, …, paramN) => { statements } (param1, param2, …, paramN) => expression     // 等于 :  => { return expression; } // 只有一个参数时,括号才可以不加:(singleParam) => { statements }singleParam => { statements }//如果没有参数,就一定要加括号:() => { statements }

var simple = a => a > 15 ? 15 : a; simple(16); // 15simple(10); // 10let max = (a, b) => a > b ? a : b;// Easy array filtering, mapping, ...var arr = [5, 6, 13, 0, 1, 18, 23];var sum = arr.reduce((a, b) => a + b);  // 66var even = arr.filter(v => v % 2 == 0); // [6, 0, 18]var double = arr.map(v => v * 2);       // [10, 12, 26, 0, 2, 36, 46]

function MakePowerFn(power) {  return function PowerFn(base) {    return Math.pow(base, power);  } }power3 = MakePowerFn(3); //制造一个X的3次方的函数power2 = MakePowerFn(2); //制造一个X的2次方的函数console.log(power3(10)); //10的3次方 = 1000console.log(power2(10)); //10的2次方 = 100

function MakePowerFn(power) {  return function(base) {    return Math.pow(base, power);  } }

MakePowerFn = power  => {  return base => {    return Math.pow(base, power);  } }

MakePowerFn = power => base => Math.pow(base, power)

MakePowerFn = (power) => (  (base) => (Math.pow(base, power)))

function fact(n){  return n==0 ? 1 :  n * fact(n-1);};result = fact(5);

function combinator(func) {  func(func);}

（func) => (func(func))

function fact(func, n) {  return n==0 ? 1 :  n * func(func, n-1);}fact(fact, 5); //输出120

var fact = (func, n) => ( n==0 ? 1 :  n * func(func, n-1) )fact(fact, 5)

(func, n) => ( n==0 ? 1 :  n * func(func, n-1) )

(func, x) => func(func, x)

( (func, x) => func(func, x) ) (  //函数体  (func, n) => ( n==0 ? 1 :  n * func(func, n-1) ), //第一个调用参数  5 //第二调用参数);

HighOrderFact = function(func){  return function(n){    return n==0 ? 1 : n * func(func)(n-1);  };};

fact = HighOrderFact(HighOrderFact);fact(5);

HighOrderFact ( HighOrderFact ) ( 5 )

fact = function ( hifunc ) {  return hifunc ( hifunc );} (  //调用参数是一个函数  function (func) {     return function(n){      return n==0 ? 1 : n * func(func)(n-1);    };  });fact(5); //于是我们就可以直接使用了

fact = (highfunc => highfunc ( highfunc ) ) (  func => n =>  n==0 ? 1 : n * func(func)(n-1));

//正常的版本function find (x, y) {  for ( let i = 0; i < x.length; i++ ) {    if ( x[i] == y ) return i;  }  return null;}

function find (x, y, i=0) {  if ( i >= x.length ) return null;  if ( x[i] == y ) return i;  return find(x, y, i+1);}

( (func, x, y, i) => func(func, x, y, i) ) (  //函数体  (func, x, y, i=0) => (      i >= x.length ?  null :         x[i] == y  ?  i : func (func, x, y, i+1)  ), //第一个调用参数  arr, //第二调用参数  2 //第三调用参数)

const find = ( highfunc => highfunc( highfunc ) ) (   func => (x, y, i = 0) => (     i >= x.length ?  null :           x[i] == y  ?  i : func (func) (x, y, i+1)   ));

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 -

「若你有原创文章想与大家分享，欢迎投稿。」