45fan.com - 路饭网

搜索: 您的位置主页 > 电脑频道 > 编程代码 > 阅读资讯:js函数式编程学习笔记知识集锦

js函数式编程学习笔记知识集锦

2017-07-02 16:19:07 来源:www.45fan.com 【

js函数式编程学习笔记知识集锦

(1)平常写的函数大多是接受值,合并值,返回值,比如经常写的for循环:

function printArray(array){
 for(var i=0;i<array.length;i++){
   print(array[i]); 
 }  
}

但是如果我们想做print之外的事情呢?怎么办?再写一个相似的,未免显得浪费,我们可以这样

function forEach(array,action){
 for(var i=0;i<array.length;i++){
  action(array[i]); 
 } 
}
forEach(["a","b","c"],print);

通过利用匿名函数,在编写for循环之类的可以省去很多无用的细节:

function sum(numbers){
 var total = 0;
 forEach(numbers,function(number){
   total+=number;
 })  
 return total; 
}

上面的例子中是“遍历数组”,并使其抽象化,函数作为函数参数传入....

(2)另一种是传入函数参数,返回函数,可以在“高阶函数”中传入arguments

function negate(func){
 return function(x){
  return !func(x); 
 }
}
var isNotNaN = negate(isNaN);
isNotNaN(NaN);

如果想要反转的函数接受参数大于1个,怎么办?? 很简单,借助apply方法,上下文传入NULL

传说中的组合模式:
function compose(f1,f2){
  return function(){
   return f1(f2.apply(null,arguments));
  };
}

var isNotNaN = compose(op["!"],isNaN);
isNotNaN(5); =>true

间接函数调用,如果运行次数较多还是不要用的好..

(3)sum函数实际上是算法的一个变体,该算法通常称为规约

function reduce(combine,base,array){
 forEach(array,function(element){
  base = combine(base,element);
 });
}

function add(a,b){
 return a+b;
}

reduce(add,0,array);

(4)另外一个与数组相关的有用的基本算法称为“映射”。它能够遍历数组

function map(func,array){
 var result = [];
 forEach(array,function(element){
  result.push(func(element));
 });
 return result;
}

map(Math.round,[0.01,2,9,Math.PI]);

(5)下面这段代码,可以研究下它的工作原理

function splitParagraph(text){
 function split(pos){
   if(pos == text.length) return [];
   else if(text.charAt(pos) == "*"){
     var end = findClosing(“*”,pos+1);
     frag = {type:“emphasized”,content:text.slice(pos+1,end)};
     return [frag].concat(split(end+1)); //回调
   } else if(text.charAt(pos) == "{"){
     var end = findClosing(“{”,pos+1);
     frag = {type:“emphasized”,content:text.slice(pos+1,end)};
     return [frag].concat(split(end+1)); //回调
   } else{
    var end = findOpeningOrEnd(pos),
      frag = {type:"normal",content:text.splice(pos+1,end)};
      return [frag].concat(split(end));

   }
 }
 function findClosing(character,from){
   var end = text.indexOf(character,from);
   if(end == -1) throw new Error("Missing closing ' "+character+"'");
   return end;
 }
 function findOpeningOrEnd(from){
   function indexOrEnd(character){
    var index = text.indexOf(character,from);
    return index = -1?text.length:index;
   }
   return Math.min(indexOrEnd("*"),indexOrEnd("{"));
 }
 
 return split(0); 
}

这种函数的编程风格很独特,使用递归而不是循环,其实递归效率是比较低的,改进如下:

function split(){
  var pos = 0,fragments = [];
  while(pop<text.lenght){
   if(text.charAt(pos) == "*"){
     var end = findClosing("*",pos+1);
     fragments.push({type:"emphasized",content:text.slice(pos+1,end)});
     pos = end+1;
   }else if(text.charAt(pos) == "{"){
     var end = findClosing("}",pos+1);
     fragments.push({type:"footnote",content:text.slice(pos+1,end)});
   }
   else{
     var end = findOpeningOrEnd(pos);
     fragments.push({type:“footnote”,content:text.slice(pos,end)});
     pos = end;
   }

  }
  return fragments;
}

(6)分布应用模式

function partial(func){
  var knownArgs = arguments;
  return function(){
   var realArgs = [];
   for(var i=1;i<knownArgs.length;i++){ //from 1
    realArgs.push(knowArgs[i]);
   }
   for(var i=0;i<arguments.length;i++){
    realArgs.push(arguments[i]); 
   }
   return func.apply(null,realArgs);
  }
}

map(partial(op["+"],1),[0,2,4,6,8,10]); // op["+"] swithcase 的一个function

本文地址:http://www.45fan.com/bcdm/89429.html
Tags: 学习 编程 数式
编辑:路饭网
推广内容
推荐阅读
热门推荐
推荐文章
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部