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
相关推荐
Jruing 2020-11-01
89231645 2020-10-26
87204154 2020-09-24
81244053 2020-09-23
FalseNotFalse 2020-09-22
81540398 2020-09-04
84423067 2020-06-12
hongbing 2020-06-02
huavhuahua 2020-05-11
samsai00 2020-05-06
猛禽的编程艺术 2020-04-23
上海滩 2020-04-22
斑点喵 2020-03-04
cuiguanjun 2020-03-01
TheBigBlue 2020-02-20
banzhihuanyu 2020-02-19
banzhihuanyu 2020-02-15
89510196 2020-02-06
banzhihuanyu 2020-01-31