javascript(函数式编程思考) ---> Map-Filter-quicksort-Collatz序列-Flodl-Flodr
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的更多相关文章
- Python之路Python作用域、匿名函数、函数式编程、map函数、filter函数、reduce函数
Python之路Python作用域.匿名函数.函数式编程.map函数.filter函数.reduce函数 一.作用域 return 可以返回任意值例子 def test1(): print(" ...
- 转:JavaScript函数式编程(三)
转:JavaScript函数式编程(三) 作者: Stark伟 这是完结篇了. 在第二篇文章里,我们介绍了 Maybe.Either.IO 等几种常见的 Functor,或许很多看完第二篇文章的人都会 ...
- 转: JavaScript函数式编程(二)
转: JavaScript函数式编程(二) 作者: Stark伟 上一篇文章里我们提到了纯函数的概念,所谓的纯函数就是,对于相同的输入,永远会得到相同的输出,而且没有任何可观察的副作用,也不依赖外部环 ...
- 转:JavaScript函数式编程(一)
转:JavaScript函数式编程(一) 一.引言 说到函数式编程,大家可能第一印象都是学院派的那些晦涩难懂的代码,充满了一大堆抽象的不知所云的符号,似乎只有大学里的计算机教授才会使用这些东西.在曾经 ...
- JavaScript 函数式编程读书笔记1
概述 这是我读<javascript函数式编程>的读书笔记,供以后开发时参考,相信对其他人也有用. 说明:虽然本书是基于underscore.js库写的,但是其中的理念和思考方式都讲的很好 ...
- 一文带你了解JavaScript函数式编程
摘要: 函数式编程入门. 作者:浪里行舟 Fundebug经授权转载,版权归原作者所有. 前言 函数式编程在前端已经成为了一个非常热门的话题.在最近几年里,我们看到非常多的应用程序代码库里大量使用着函 ...
- JavaScript 函数式编程读书笔记2
概述 这是我读<javascript函数式编程>的读书笔记,供以后开发时参考,相信对其他人也有用. 说明:虽然本书是基于underscore.js库写的,但是其中的理念和思考方式都讲的很好 ...
- 函数式编程工具:filter和reduce
# -*- coding: utf-8 -*- #python 27 #xiaodeng #函数式编程工具:filter和reduce #python内置函数中,map函数是用来进行函数式编程这类工具 ...
- javascript函数式编程和链式优化
1.函数式编程理解 函数式编程可以理解为,以函数作为主要载体的编程方式,用函数去拆解.抽象一般的表达式 与命令式相比,这样做的好处在哪?主要有以下几点: (1)语义更加清晰 (2)可复用性更高 (3) ...
随机推荐
- w3c网址和标准化过程
- [題解](縮點)luogu_P2341受歡迎的牛
對於每個強聯通分量,這些牛一定都互相喜歡,所以縮點(我也不知道怎麼想到的) 接下來就是統計答案,最後縮成了一個DAG圖,如果這個點是明星的話,其他每個點一定直接或間接的鏈接這個點 也就是說其他點一定有 ...
- Day2课后作业:三级菜单简单版
menu = { '北京':{ '海淀':{ '五道口':{ 'soho':{}, '网易':{}, '谷歌':{} }, '中关村':{ '爱奇艺':{}, '汽车之家':{}, 'youku':{ ...
- 记一下一道关于finally的题
题目: public class Test{ public int add(int a,int b){ try { return a+b; } catch (Exception e) { Syste ...
- 1047 - Best couple 好题~
http://www.ifrog.cc/acm/problem/1047 思路很简单,跑一发floyd,然后再用km. 但是问题来了,这个有可能n != m.那怎么办? 其实可以补上一些不存在的点.来 ...
- Memcached 未授权访问漏洞及加固
memcached是一套分布式的高速缓存系统.它以Key-Value(键值对)形式将数据存储在内存中,这些数据通常是应用读取频繁的.正因为内存中数据的读取远远大于硬盘,因此可以用来加速应用的访问. 漏 ...
- Nodejs chrome 调试node-inspector
1.下载扩展: 全局安装 npm install -g node-inspector 2.开启debug调试: node --debug[=port] filename (默认端口5858)node ...
- 安卓,IOS真机调试
移动端前端开发真机调试攻略 有线调试: 一.IOS 移动端 (Safari开发者工具) 手机端:设置 → Safari → 高级 → Web 检查器 → 开. mac端:Safari → 偏好设置 → ...
- asp。Net 页面传值
00.引言 Web页面是无状态的, 服务器对每一次请求都认为来自不同用户,因此,变量的状态在连续对同一页面的多次请求之间或在页面跳转时不会被保留.在用ASP.NET 设计开发一个Web系统时, 遇到一 ...
- promise从易到难
Chapter 1 // 需求:你要封装一个方法,我给你一个要读取文件的路径,你这个方法能帮我读取文件,并把内容返回给我 const fs = require('fs') const path = r ...