let add = x=>x+1;

//Map :: ( a -> b) -> [a] -> [b]
let Map = function(f,arr){
//闭包存储累积对象
let result = [];
return function map(f,arr){
if(!Array.isArray(arr)){
return "要处理的对象为数组";
}
if(arr.length ==0){
return [];
}else{
let [head,...tail] = arr;
result.push(f(head));
// 通过回调map遍历目标对象arr,f处理过的元素存入result,tail为[]即遍历结束,可以返回result
return tail.length == 0 ? result : map(f,tail);
}
}(f,arr);
};
let test = Map(add,[1,2,3]);
console.log(test);//[2,3,4]
let biggerTen = x => x>10;
//Filter :: (a -> Bool) -> [a] -> [a]
let Filter = function(f,arr){
let result=[];
return function filter(f,arr){
if(!Array.isArray(arr)){
return "要处理的对象为数组";
}
if(arr.length == 0){
return [];
}else{
let [head,...tail] = arr;
if(f(head)){
result.push(head)
}
return tail.length == 0 ? result : filter(f,tail);
}
}(f,arr);
}; let testFilter = Filter(biggerTen,[1,5,10,44,12,2,5]);
console.log(testFilter);//[44,12]

快速排序:

let small  = x=>y=>y <=x;
let bigger = x=>y=>y > x;
console.log(Filter(bigger(5),[7,2,1,4]));
console.log(Filter(small(5),[7,2,1,4])); //QuickSort :: (Ord a) => [a] -> [a]
let QuickSort = (function(){
return function quickSort(arr){
if(arr.length == 0){
return [];
}else{
let [head,...tail] = arr;
let smallerSorted = quickSort(Filter(small(head),tail));
let biggerSorted = quickSort(Filter(bigger(head),tail));
return smallerSorted.concat([head]).concat(biggerSorted);
}
};
})(); let testQuickSort = QuickSort([5,7,2,1,4]);
console.log(testQuickSort);//[1,2,4,5,7]

Collatz序列:

let div2  = x=>x/2;
let someF = x=>x*3+1; const Chain = function(n){
let result=[];
return function chain(n){
result.push(n);
if(n == 1){return [1]}
let even = n%2 ==0 ? chain( div2(n) ) : [];
let odd = n%2 !=0 ? chain( someF(n)) : [];
return result;
}(n);
};
console.log(Chain(10));//[10,5,16,8,4,2,1]

Foldl:

// 左折叠
const Foldl = function(f,list,initValue){
if(list.length == 0){
return initValue || null;
}else{
let accumulated=initValue || list.shift() ;
let foldl = function(acc,x){
accumulated = f(acc,x);
return list.length==0 ? accumulated : foldl(accumulated,list.shift());
}
return list.length==0 ? accumulated : foldl(accumulated,list.shift());
}
} let reducer = (x,y) => (x+y);
let reducerMultiply = (x,y) => x*y;
console.log(Foldl(reducer,[1,2,3]));//
console.log(Foldl(reducerMultiply,[2,3,4]));//

Foldr:

// 右折叠
const Foldr = function(f,list,initValue){
if(list.length == 0){
return initValue || null;
}else {
let accumulated = initValue || list.pop();
let foldr = function(x,acc){
accumulated = f(x,acc);
return list.length==0 ? accumulated : foldr(list.pop(),accumulated);
}
return list.length==0 ? accumulated : foldr(list.pop(),accumulated);
}
} let reverse = (x,y) => {
y.push(x);
return y;
}; console.log(Foldr(reverse,[1,2,3],[]));//[3,2,1]

用Fold重新创建新的Map和Filter:

// 用Foldr创建新的Filter 当然也可以用Foldl
const newFilter = function(f,list){
let condition = function(x,acc){
f(x) && acc.push(x);
return acc;
}
let result = Foldr(condition,list,[]);
return Foldr(reverse,result,[]);
} let bigger20 = x=>x>20; console.log(newFilter(bigger20,[10,22,50,1,41,2])); // [22,50,41] // 用Foldl 创建新的Map
const newMap = function(f,list){
let map = function(acc,x){
acc.push(f(x));
return acc;
}
return Foldl(map,list,[]);
}
let addTwo = x => x+2;
console.log(newMap(addTwo,[1,2,3]));//[3,4,5]

javascript(函数式编程思考) ---> Map-Filter-quicksort-Collatz序列-Flodl-Flodr的更多相关文章

  1. Python之路Python作用域、匿名函数、函数式编程、map函数、filter函数、reduce函数

    Python之路Python作用域.匿名函数.函数式编程.map函数.filter函数.reduce函数 一.作用域 return 可以返回任意值例子 def test1(): print(" ...

  2. 转:JavaScript函数式编程(三)

    转:JavaScript函数式编程(三) 作者: Stark伟 这是完结篇了. 在第二篇文章里,我们介绍了 Maybe.Either.IO 等几种常见的 Functor,或许很多看完第二篇文章的人都会 ...

  3. 转: JavaScript函数式编程(二)

    转: JavaScript函数式编程(二) 作者: Stark伟 上一篇文章里我们提到了纯函数的概念,所谓的纯函数就是,对于相同的输入,永远会得到相同的输出,而且没有任何可观察的副作用,也不依赖外部环 ...

  4. 转:JavaScript函数式编程(一)

    转:JavaScript函数式编程(一) 一.引言 说到函数式编程,大家可能第一印象都是学院派的那些晦涩难懂的代码,充满了一大堆抽象的不知所云的符号,似乎只有大学里的计算机教授才会使用这些东西.在曾经 ...

  5. JavaScript 函数式编程读书笔记1

    概述 这是我读<javascript函数式编程>的读书笔记,供以后开发时参考,相信对其他人也有用. 说明:虽然本书是基于underscore.js库写的,但是其中的理念和思考方式都讲的很好 ...

  6. 一文带你了解JavaScript函数式编程

    摘要: 函数式编程入门. 作者:浪里行舟 Fundebug经授权转载,版权归原作者所有. 前言 函数式编程在前端已经成为了一个非常热门的话题.在最近几年里,我们看到非常多的应用程序代码库里大量使用着函 ...

  7. JavaScript 函数式编程读书笔记2

    概述 这是我读<javascript函数式编程>的读书笔记,供以后开发时参考,相信对其他人也有用. 说明:虽然本书是基于underscore.js库写的,但是其中的理念和思考方式都讲的很好 ...

  8. 函数式编程工具:filter和reduce

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #函数式编程工具:filter和reduce #python内置函数中,map函数是用来进行函数式编程这类工具 ...

  9. javascript函数式编程和链式优化

    1.函数式编程理解 函数式编程可以理解为,以函数作为主要载体的编程方式,用函数去拆解.抽象一般的表达式 与命令式相比,这样做的好处在哪?主要有以下几点: (1)语义更加清晰 (2)可复用性更高 (3) ...

随机推荐

  1. PJzhang:国内常用威胁情报搜索引擎说明

    猫宁!!! 参考链接: https://www.freebuf.com/column/136763.html https://www.freebuf.com/sectool/163946.html 如 ...

  2. Kera高层API

    目录 Keras != tf.keras Outline1 Metrics Step1.Build a meter Step2.Update data Step3.Get Average data C ...

  3. android 一些常用权限

    <!-- 网络访问权限 --> <uses-permission android:name="android.permission.INTERNET" /> ...

  4. SpringBoot | idea新建项目

    1.new ----> Spring Initializr 2.设置相应文件名 3.选择需要配置

  5. iOS蓝牙连接流程介绍-1

    蓝牙连接流程介绍 1.1-程序员找女朋友流程介绍 0.程序员找女朋友参与者 1.你 2.受害者(女性同胞)  (1)她的性格1 性格的特点 (2)她的性格2  分析性格的特点 1.寻找女性 寻尽身边一 ...

  6. TYVJ 2032 搜索

    P2032 「Poetize9」升降梯上 描述 开启了升降梯的动力之后,探险队员们进入了升降梯运行的那条竖直的隧道,映入眼帘的是一条直通塔顶的轨道.一辆停在轨道底部的电梯.和电梯内一杆控制电梯升降的巨 ...

  7. 牛客寒假6-C.项链

    链接:https://ac.nowcoder.com/acm/contest/332/C 题意: 小B想给她的新项链染色. 现在有m种颜色,对于第i种颜色,小B有a_i单位的颜料,每单位颜料可以染项链 ...

  8. HDU - 6312( 2018 Multi-University Training Contest 2)

    bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6312 输出前几项,都是"Yes" #include <bits ...

  9. 【aspnetcore】异常捕捉可用知识点

    1.使用过滤器ExceptionFilter:补充:常用过滤器:AuthorizationFilter.ActionFilter.ResultFilter.ResourceFilter.Excepti ...

  10. DB2 函数

    1.大小写转换 转大写UPPER 转小写LOWER