一份用 ES6 写的 JavaScript 函数式编程手册
Translations
相同的参数返回相同的结果,它的执行不依赖于系统的状态
- 非纯净的
let number = 1;
const increment = () => number += 1;
increment();
// 2
- 纯净的
const increment = n => n + 1;
increment(1);
// 2
函数把其他函数当做参数传递使用或者返回一个函数
- 加法
const sum = (x, y) => x + y;
const calculate = (fn, x, y) => fn(x, y);
calculate(sum, 1, 2);
// 3
- filter
let students = [
{name: 'Anna', grade: 6},
{name: 'John', grade: 4},
{name: 'Maria', grade: 9}
];
const isApproved = student => student.grade >= 6;
students.filter(isApproved);
// [ { name: 'Anna', grade: 6 }, { name: 'Maria', grade: 9 } ]
- Map
const byName = obj => obj.name;
students.map(byName);
// [ 'Anna', 'John', 'Maria' ]
- 链式
let students = [
{name: 'Anna', grade: 6},
{name: 'John', grade: 4},
{name: 'Maria', grade: 9}
];
const isApproved = student => student.grade >= 6;
const byName = obj => obj.name;
students.filter(isApproved).map(byName);
// ['Anna', 'Maria']
- Reduce
const totalGrades = students.reduce((sum, student) => sum + student.grade, 0);
totalGrades
// 19
当一个函数调用它自己的时候,就创造了一个循环
- 递减
const countdown = num => {
console.log(num)
num < 1
? num
: countdown(num - 1)
}
countdown(5);
/*
5
4
3
2
1
0
*/
- 阶乘
const factorial = (num) =>
num <= 0
? 1
: n * factorial(num - 1)
factorial(5);
//120
有 map 方法的对象。functor 的 map 方法通过 map 回调函数调用自己的内容,然后返回一个新的 functor.
- 给数组所有的元素添加一个值
const plus1 = num => num + 1;
let numbers = [1, 2, 3];
numbers.map(plus1);
// [2, 3, 4]
通过组合两个或更多的函数生成一个新的函数
- 组合两个函数生成一个新的函数
const compose = (f,g) => x => f(g(x));
const toUpperCase = x => x.toUpperCase();
const exclaim = x => `${x}!`;
const angry = compose(exclaim, toUpperCase);
angry("stop this");
// STOP THIS!
- 组合三个函数生成一个新的
const compose = (f,g) => x => f(g(x));
const toUpperCase = x => x.toUpperCase();
const exclaim = x => `${x}!`;
const moreExclaim = x => `${x}!!!!!`;
const reallyAngry = compose(exclaim, compose(toUpperCase, moreExclaim));
reallyAngry("stop this");
// STOP THIS!!!!!!
从数组中提取数据或对象使用一种语法混合数组和对象文本的建设。或“模式匹配”。
- Select from pattern
const foo = () => [1, 2, 3];
const [a, b] = foo();
console.log(a, b);
// 1 2
- 接收 rest 值
const [a, ...b] = [1, 2, 3];
console.log(a, b);
// 1 [2, 3]
- 可选参数
const ajax = ({ url = "localhost", port: p = 80}, ...data) =>
console.log("Url:", url, "Port:", p, "Rest:", data);
ajax({ url: "someHost" }, "additional", "data", "hello");
// Url: someHost Port: 80 Rest: [ 'additional', 'data', 'hello' ]
ajax({ }, "additional", "data", "hello");
// Url: localhost Port: 80 Rest: [ 'additional', 'data', 'hello' ]
一个函数有多个参数,把每个参数通过链式的形式返回下一个函数,直到最后返回结果。
- 对象柯里化
const student = name => grade => `Name: ${name} | Grade: ${grade}`;
student("Matt")(8);
// Name: Matt | Grade: 8
- 加法函数柯里化
const add = x => y => x + y;
const increment = add(1);
const addFive = add(5);
increment(3);
//4
addFive(10);
// 15
https://gist.github.com/mikaelbr/9900818
https://www.gitbook.com/book/jcouyang/functional-javascript/details
https://www.youtube.com/playlist?list=PL0zVEGEvSaeEd9hlmCXrk5yUyqUag-n84
http://functionaljavascript.blogspot.com.br/2013/07/functors.html
http://nicoespeon.com/en/2015/01/pure-functions-javascript/